All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.emul.java.util.TreeSet Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package java.util;

import java.io.Serializable;

/**
 * Implements a set using a TreeMap. [Sun
 * docs]
 * 
 * @param  element type.
 */
public class TreeSet extends AbstractSet implements SortedSet, Serializable {

  /**
   * TreeSet is stored as a TreeMap of the requested type to a constant Boolean.
   */
  private SortedMap map;

  public TreeSet() {
    map = new TreeMap();
  }

  public TreeSet(Collection c) {
    this();
    addAll(c);
  }

  public TreeSet(Comparator c) {
    if (c == null) {
      map = new TreeMap();
    } else {
      map = new TreeMap(c);
    }
  }

  public TreeSet(SortedSet s) {
    this(s.comparator());
    // TODO(jat): more efficient implementation
    addAll(s);
  }

  /**
   * Used to wrap subset maps in a new TreeSet.
   * 
   * @param map map to use for backing store
   */
  private TreeSet(SortedMap map) {
    this.map = map;
  }

  @Override
  public boolean add(E o) {
    // Use Boolean.FALSE as a convenient non-null value to store in the map
    return map.put(o, Boolean.FALSE) == null;
  }

  @Override
  public void clear() {
    map.clear();
  }

  public Comparator comparator() {
    return map.comparator();
  }

  @Override
  public boolean contains(Object o) {
    return map.containsKey(o);
  }

  public E first() {
    return map.firstKey();
  }

  public SortedSet headSet(E toElement) {
    return new TreeSet(map.headMap(toElement));
  }

  @Override
  public Iterator iterator() {
    return map.keySet().iterator();
  }

  public E last() {
    return map.lastKey();
  }

  @Override
  public boolean remove(Object o) {
    return map.remove(o) != null;
  }

  @Override
  public int size() {
    return map.size();
  }

  public SortedSet subSet(E fromElement, E toElement) {
    return new TreeSet(map.subMap(fromElement, toElement));
  }

  public SortedSet tailSet(E fromElement) {
    return new TreeSet(map.tailMap(fromElement));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy