001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.corpus.penntreebank;
025
026import java.util.ArrayList;
027import java.util.Iterator;
028import java.util.List;
029import java.util.SortedSet;
030import java.util.TreeSet;
031
032import com.google.common.annotations.Beta;
033
034/**
035 * <br>
036 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
037 * All rights reserved.
038 * 
039 * 
040 * @author Philipp Wetzler
041 * 
042 */
043
044@Beta
045public class ListSpecification implements Iterable<Integer> {
046
047  private List<RangeSpecification> ranges;
048
049  private SortedSet<Integer> numbers = new TreeSet<Integer>();
050
051  public ListSpecification(String s) {
052    this.ranges = new ArrayList<RangeSpecification>();
053
054    for (String rangeSpec : s.trim().split(",")) {
055      if (rangeSpec.length() > 0)
056        this.ranges.add(new RangeSpecification(rangeSpec));
057    }
058
059    for (RangeSpecification r : ranges)
060      for (Integer i : r)
061        numbers.add(i);
062
063  }
064
065  public boolean contains(int i) {
066    return numbers.contains(i);
067  }
068
069  @Override
070  public String toString() {
071    StringBuffer buffer = new StringBuffer();
072    boolean first = true;
073
074    for (RangeSpecification range : this.ranges) {
075      if (!first)
076        buffer.append(",");
077
078      buffer.append(range);
079      first = false;
080    }
081
082    return buffer.toString();
083  }
084
085  public Iterator<Integer> iterator() {
086    return numbers.iterator();
087  }
088}