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 2017 araguacaima
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.araguacaima.commons.utils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.stream.Collectors;
public class MapUtils {
public static final int EVALUATE_BOTH_KEY_AND_VALUE = 0;
public static final int DEFAULT_EVALUATION_TYPE = EVALUATE_BOTH_KEY_AND_VALUE;
public static final int EVALUATE_BOTH_KEY_OR_VALUE = 3;
public static final int EVALUATE_JUST_KEY = 1;
public static final int EVALUATE_JUST_VALUE = 2;
public static final StringKeyHashMapUtil stringKeyHashMapUtil = new StringKeyHashMapUtil();
private static final Logger log = LoggerFactory.getLogger(MapUtils.class);
private static final MapUtils INSTANCE = new MapUtils();
private MapUtils() {
if (INSTANCE != null) {
throw new IllegalStateException("Already instantiated");
}
}
public static MapUtils getInstance() {
return INSTANCE;
}
public static Map fromProperties(final Properties properties) {
final Map map = new HashMap<>();
IterableUtils.forEach(properties.keySet(), key -> {
if (key != null) {
Object value = properties.get(key);
if (value != null) {
value = String.valueOf(value);
} else {
value = StringUtils.EMPTY;
}
map.put((String) key, (String) value);
}
});
return map;
}
public static boolean isEmpty(Map map) {
return org.apache.commons.collections4.MapUtils.isEmpty(map);
}
public static boolean isNotEmpty(Map> map) {
return org.apache.commons.collections4.MapUtils.isNotEmpty(map);
}
public static Map clone(Map original) {
return original.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cannot clone instance of this class");
}
public Map find(Map map, Predicate keyPredicate, Predicate valuePredicate, int evaluationType) {
Map newMap = new HashMap<>();
E key;
T value;
for (Map.Entry entry : map.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (existsInMap(key, value, keyPredicate, valuePredicate, evaluationType)) {
newMap.put(key, value);
break;
}
}
return newMap;
}
public boolean existsInMap(E key,
T value,
Predicate keyPredicate,
Predicate valuePredicate,
int evaluationType) {
boolean result = false;
try {
switch (evaluationType) {
case EVALUATE_JUST_KEY:
result = keyPredicate != null && keyPredicate.evaluate(key);
break;
case EVALUATE_JUST_VALUE:
result = valuePredicate != null && valuePredicate.evaluate(value);
break;
case EVALUATE_BOTH_KEY_AND_VALUE:
result = keyPredicate != null && keyPredicate.evaluate(key) && valuePredicate != null &&
valuePredicate.evaluate(
value);
break;
case EVALUATE_BOTH_KEY_OR_VALUE:
result = keyPredicate != null && keyPredicate.evaluate(key) || valuePredicate != null &&
valuePredicate.evaluate(
value);
break;
default:
break;
}
} catch (Exception e) {
log.debug("Error evaluating predicates. " + e.getMessage(), 1);
}
return result;
}
public Object findObject(Map map,
Predicate keyPredicate,
Predicate valuePredicate,
int evaluationType) {
E key;
T value = null;
for (Map.Entry entry : map.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (existsInMap(key, value, keyPredicate, valuePredicate, evaluationType)) {
break;
}
}
return value;
}
public void removeAll(final Map, ?> map, Collection> keys) {
IterableUtils.forEach(keys, map::remove);
}
public Map, ?> select(Map map, Predicate keyPredicate, Predicate valuePredicate) {
Map