com.dasasian.chok.util.CollectionUtil Maven / Gradle / Ivy
/**
* Copyright (C) 2014 Dasasian ([email protected])
*
* 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.dasasian.chok.util;
import java.util.*;
public class CollectionUtil {
// TODO jz: avoid with bidi map?
public static Map> invertListMap(Map> key2ValuesMap) {
One2ManyListMap value2KeysMap = new One2ManyListMap<>();
for (K shardName : key2ValuesMap.keySet()) {
List nodes = key2ValuesMap.get(shardName);
for (V node : nodes) {
value2KeysMap.add(node, shardName);
}
}
return value2KeysMap.asMap();
}
public static List getListOfAdded(final Collection oldList, final Collection updatedList) {
final List addedEntriesList = new ArrayList<>();
extractAddedEntries(oldList, updatedList, addedEntriesList);
return addedEntriesList;
}
public static Set getSetOfAdded(final Collection oldSet, final Collection updatedSet) {
final Set addedEntriesSet = new HashSet<>();
extractAddedEntries(oldSet, updatedSet, addedEntriesSet);
return addedEntriesSet;
}
public static List getListOfRemoved(final Collection oldList, final Collection updatedList) {
final List removedEntriesList = new ArrayList<>();
extractRemovedEntries(oldList, updatedList, removedEntriesList);
return removedEntriesList;
}
public static Set getSetOfRemoved(final Collection oldSet, final Collection updatedSet) {
final Set removedEntriesSet = new HashSet<>();
extractRemovedEntries(oldSet, updatedSet, removedEntriesSet);
return removedEntriesSet;
}
private static void extractAddedEntries(final Collection oldCollection, final Collection updatedCollection, final Collection addedEntriesCollection) {
for (final String entry : updatedCollection) {
if (!oldCollection.contains(entry)) {
addedEntriesCollection.add(entry);
}
}
}
private static void extractRemovedEntries(final Collection oldCollection, final Collection updatedCollection, final Collection removedEntriesCollection) {
for (final String string : oldCollection) {
if (!updatedCollection.contains(string)) {
removedEntriesCollection.add(string);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy