
net.oneandone.troilus.Immutables Maven / Gradle / Ivy
/*
* Copyright 1&1 Internet AG, https://github.com/1and1/
*
* 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 net.oneandone.troilus;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
/**
* Immutable utility class
*/
class Immutables {
/**
* merges a new entry into a set
*
* @param set the set
* @param entryToAdd the entry to add
* @return the new merged immutable set
*/
public static ImmutableSet join(ImmutableSet set, T entryToAdd) {
return ImmutableSet.builder().addAll(set).add(entryToAdd).build();
}
/**
* merges 2 sets
*
* @param set1 the set1 to merge
* @param set2 the set2 to merge
* @return the new merged immutable set
*/
public static ImmutableSet join(ImmutableSet set1, ImmutableSet set2) {
return ImmutableSet.builder().addAll(set1).addAll(set2).build();
}
/**
* merges a new entry into a list
*
* @param list the list to merge
* @param entryToAdd the entry to add
* @return the new merged immutable list
*/
public static ImmutableList join(ImmutableList list, T entryToAdd) {
return ImmutableList.builder().addAll(list).add(entryToAdd).build();
}
/**
* merges a new entry into a list
*
* @param list the list to merge
* @param entryToAdd the entry to add
* @return the new merged immutable list
*/
public static ImmutableList join(T entryToAdd, ImmutableList list) {
return ImmutableList.builder().add(entryToAdd).addAll(list).build();
}
/**
* merges 2 lists
*
* @param list1 the list1 to merge
* @param list2 the list2 to merge
* @return the new merged immutable list
*/
public static ImmutableList join(ImmutableList list1, ImmutableList list2) {
return ImmutableList.builder().addAll(list1).addAll(list2).build();
}
/**
* merges a new entry into a map
*
* @param map the map to merge
* @param key the key of the new entry
* @param value the value of the new entry
* @return the new merged immutable map
*/
public static ImmutableMap join(ImmutableMap map, K key, V value) {
Map m = Maps.newHashMap(map);
m.put(key, value);
return ImmutableMap.copyOf(m);
}
/**
* merges 2 maps
*
* @param map1 the map1 to merge
* @param map2 the map2 to merge
* @return the new merged immutable map
*/
public static ImmutableMap join(ImmutableMap map1, ImmutableMap map2) {
return ImmutableMap.builder().putAll(map1).putAll(map2).build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy