001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.util;
018
019 import com.google.common.base.Supplier;
020 import com.google.common.collect.*;
021
022 import java.util.Collection;
023 import java.util.List;
024 import java.util.Set;
025
026 public class CommonSuppliers {
027 private CommonSuppliers() {}
028
029 private static final Supplier<?> ARRAY_LIST_SUPPLIER = new Supplier() {
030 @Override
031 public List get() {
032 return Lists.newArrayList();
033 }
034 };
035
036 private static final Supplier<?> LINKED_HASH_SET_SUPPLIER = new Supplier() {
037 @Override
038 public Set get() {
039 return Sets.newLinkedHashSet();
040 }
041 };
042
043 private static final Supplier<?> HASH_SET_SUPPLIER = new Supplier() {
044 @Override
045 public Set get() {
046 return Sets.newHashSet();
047 }
048 };
049
050 public static <T> Supplier<List<T>> getArrayListSupplier() {
051 //noinspection unchecked
052 return (Supplier<List<T>>) ARRAY_LIST_SUPPLIER;
053 }
054
055 public static <T> Supplier<Set<T>> getLinkedHashSetSupplier() {
056 //noinspection unchecked
057 return (Supplier<Set<T>>) LINKED_HASH_SET_SUPPLIER;
058 }
059
060 public static <T> Supplier<Set<T>> getHashSetSupplier() {
061 //noinspection unchecked
062 return (Supplier<Set<T>>) HASH_SET_SUPPLIER;
063 }
064
065 public static <K, V> SetMultimap<K, V> newLinkedHashSetHashSetMultimap() {
066 return Multimaps.newSetMultimap(Maps.<K, Collection<V>>newHashMap(), CommonSuppliers.<V>getLinkedHashSetSupplier());
067 }
068 }