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

com.google.common.collect.testing.google.SortedMapGenerators Maven / Gradle / Ivy

/*
 * Copyright (C) 2009 The Guava Authors
 *
 * 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.common.collect.testing.google;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.testing.Helpers.mapEntry;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.TestListGenerator;
import com.google.common.collect.testing.TestStringListGenerator;
import com.google.common.collect.testing.TestStringSortedMapGenerator;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;

/**
 * Generators of sorted maps and derived collections.
 *
 * @author Kevin Bourrillion
 * @author Jesse Wilson
 * @author Jared Levy
 * @author Hayward Chan
 * @author Chris Povirk
 * @author Louis Wasserman
 */
@GwtCompatible
public class SortedMapGenerators {
  public static class ImmutableSortedMapGenerator extends TestStringSortedMapGenerator {
    @Override
    public SortedMap create(Entry[] entries) {
      ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
      for (Entry entry : entries) {
        checkNotNull(entry);
        builder.put(entry.getKey(), entry.getValue());
      }
      return builder.build();
    }
  }

  public static class ImmutableSortedMapCopyOfEntriesGenerator
      extends TestStringSortedMapGenerator {
    @Override
    public SortedMap create(Entry[] entries) {
      return ImmutableSortedMap.copyOf(Arrays.asList(entries));
    }
  }

  public static class ImmutableSortedMapEntryListGenerator
      implements TestListGenerator> {

    @Override
    public SampleElements> samples() {
      return new SampleElements<>(
          mapEntry("foo", 5),
          mapEntry("bar", 3),
          mapEntry("baz", 17),
          mapEntry("quux", 1),
          mapEntry("toaster", -2));
    }

    @SuppressWarnings("unchecked")
    @Override
    public Entry[] createArray(int length) {
      return new Entry[length];
    }

    @Override
    public Iterable> order(List> insertionOrder) {
      return new Ordering>() {
        @Override
        public int compare(Entry left, Entry right) {
          return left.getKey().compareTo(right.getKey());
        }
      }.sortedCopy(insertionOrder);
    }

    @Override
    public List> create(Object... elements) {
      ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
      for (Object o : elements) {
        @SuppressWarnings("unchecked")
        Entry entry = (Entry) o;
        builder.put(entry);
      }
      return builder.build().entrySet().asList();
    }
  }

  public static class ImmutableSortedMapKeyListGenerator extends TestStringListGenerator {
    @Override
    protected List create(String[] elements) {
      ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
      for (int i = 0; i < elements.length; i++) {
        builder.put(elements[i], i);
      }
      return builder.build().keySet().asList();
    }

    @Override
    public List order(List insertionOrder) {
      return Ordering.natural().sortedCopy(insertionOrder);
    }
  }

  public static class ImmutableSortedMapValueListGenerator extends TestStringListGenerator {
    @Override
    protected List create(String[] elements) {
      ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
      for (int i = 0; i < elements.length; i++) {
        builder.put(i, elements[i]);
      }
      return builder.build().values().asList();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy