Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015 the original author or 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 org.bremersee.utils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang3.Validate;
/**
*
* Some methods to work with maps.
*
*
* @author Christian Bremer
*/
public abstract class MapUtils {
private MapUtils() {
// utility class, never constructed
}
/**
*
* A map entry comparator.
*
*
* @author Christian Bremer
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static class MapEntryComparator implements Comparator> {
private final Comparator valueComparator;
/**
* Create a map entry comparator with the specified value comparator.
*
* If the value comparator is {@code null}, the natural sort order will
* be used.
*
* @param valueComparator
* a value comparator or {@code null}
*/
public MapEntryComparator(Comparator> valueComparator) {
this.valueComparator = valueComparator;
}
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Entry, ?> e1, Entry, ?> e2) {
int c = 0;
if (e1 != null && e1.getValue() != null && e2 != null && e2.getValue() != null) {
Object value1 = e1.getValue();
Object value2 = e2.getValue();
if (valueComparator != null) {
c = valueComparator.compare(value1, value2);
} else if (value1 instanceof Comparable && value2 instanceof Comparable) {
c = ((Comparable) value1).compareTo((Comparable) value2);
}
}
if (c == 0) {
c = -1;
}
return c;
}
}
/**
* Sort the map by the values natural sort order and return an unmodifiable
* map.
*
* @param map
* the map
* @return the sorted map
*/
public static Map sort(Map extends K, ? extends V> map) {
return sort(map, null);
}
/**
* Sort the map by it's values and return an unmodifiable map.
*
* @param map
* the map
* @param valueComparator
* a value comparator
* @return the sorted map
*/
@SuppressWarnings("unchecked")
public static Map sort(Map extends K, ? extends V> map, Comparator extends V> valueComparator) {
Validate.notNull(map, "Map must not be null.");
List> entries = new ArrayList>(map.entrySet());
Collections.sort(entries, new MapEntryComparator(valueComparator));
Map