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

com.google.firebase.database.collection.ImmutableSortedSet Maven / Gradle / Ivy

/*
 * Copyright 2017 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 com.google.firebase.database.collection;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ImmutableSortedSet implements Iterable {

  private final ImmutableSortedMap map;

  public ImmutableSortedSet(List elems, Comparator comparator) {
    this.map =
        ImmutableSortedMap.Builder.buildFrom(
            elems,
            Collections.emptyMap(),
            ImmutableSortedMap.Builder.identityTranslator(),
            comparator);
  }

  private ImmutableSortedSet(ImmutableSortedMap map) {
    this.map = map;
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof ImmutableSortedSet)) {
      return false;
    }
    ImmutableSortedSet otherSet = (ImmutableSortedSet) other;
    return map.equals(otherSet.map);
  }

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

  public boolean contains(T entry) {
    return this.map.containsKey(entry);
  }

  public ImmutableSortedSet remove(T entry) {
    ImmutableSortedMap newMap = this.map.remove(entry);
    return (newMap == this.map) ? this : new ImmutableSortedSet<>(newMap);
  }

  public ImmutableSortedSet insert(T entry) {
    return new ImmutableSortedSet<>(map.insert(entry, null));
  }

  public T getMinEntry() {
    return this.map.getMinKey();
  }

  public T getMaxEntry() {
    return this.map.getMaxKey();
  }

  public int size() {
    return this.map.size();
  }

  public boolean isEmpty() {
    return this.map.isEmpty();
  }

  public Iterator iterator() {
    return new WrappedEntryIterator<>(this.map.iterator());
  }

  public Iterator iteratorFrom(T entry) {
    return new WrappedEntryIterator<>(this.map.iteratorFrom(entry));
  }

  public Iterator reverseIteratorFrom(T entry) {
    return new WrappedEntryIterator<>(this.map.reverseIteratorFrom(entry));
  }

  public Iterator reverseIterator() {
    return new WrappedEntryIterator<>(this.map.reverseIterator());
  }

  public T getPredecessorEntry(T entry) {
    return this.map.getPredecessorKey(entry);
  }

  private static class WrappedEntryIterator implements Iterator {

    final Iterator> iterator;

    public WrappedEntryIterator(Iterator> iterator) {
      this.iterator = iterator;
    }

    @Override
    public boolean hasNext() {
      return this.iterator.hasNext();
    }

    @Override
    public T next() {
      return this.iterator.next().getKey();
    }

    @Override
    public void remove() {
      this.iterator.remove();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy