org.apache.sshd.common.util.MapEntryUtils Maven / Gradle / Ivy
/*
* 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 org.apache.sshd.common.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Supplier;
/**
* Represents an un-modifiable pair of values
*
* @author Apache MINA SSHD Project
*/
public final class MapEntryUtils {
@SuppressWarnings({"rawtypes", "unchecked"})
private static final Comparator> BY_KEY_COMPARATOR = (o1, o2) -> {
Comparable k1 = o1.getKey();
Comparable k2 = o2.getKey();
return k1.compareTo(k2);
};
private MapEntryUtils() {
throw new UnsupportedOperationException("No instance");
}
/**
* @param The {@link Comparable} key type
* @param The associated entry value
* @return A {@link Comparator} for {@link java.util.Map.Entry}-ies that
* compares the key values
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static , V> Comparator> byKeyEntryComparator() {
return (Comparator) BY_KEY_COMPARATOR;
}
public static class GenericMapPopulator> implements Supplier {
private final M map;
public GenericMapPopulator(M map) {
this.map = Objects.requireNonNull(map, "No map provided");
}
public GenericMapPopulator put(K k, V v) {
map.put(k, v);
return this;
}
public GenericMapPopulator remove(K k) {
map.remove(k);
return this;
}
public GenericMapPopulator putAll(Map extends K, ? extends V> other) {
map.putAll(other);
return this;
}
public GenericMapPopulator clear() {
map.clear();
return this;
}
@Override
public M get() {
return map;
}
}
public static class MapBuilder extends GenericMapPopulator> {
public MapBuilder() {
super(new LinkedHashMap<>());
}
@Override
public MapBuilder put(K k, V v) {
super.put(k, v);
return this;
}
@Override
public MapBuilder remove(K k) {
super.remove(k);
return this;
}
@Override
public MapBuilder putAll(Map extends K, ? extends V> other) {
super.putAll(other);
return this;
}
@Override
public MapBuilder clear() {
super.clear();
return this;
}
public Map build() {
return get();
}
public Map immutable() {
return Collections.unmodifiableMap(build());
}
public static MapBuilder builder() {
return new MapBuilder<>();
}
}
public static class NavigableMapBuilder extends GenericMapPopulator> {
public NavigableMapBuilder(Comparator super K> comparator) {
super(new TreeMap<>(Objects.requireNonNull(comparator, "No comparator provided")));
}
@Override
public NavigableMapBuilder put(K k, V v) {
super.put(k, v);
return this;
}
@Override
public NavigableMapBuilder remove(K k) {
super.remove(k);
return this;
}
@Override
public NavigableMapBuilder putAll(Map extends K, ? extends V> other) {
super.putAll(other);
return this;
}
@Override
public NavigableMapBuilder clear() {
super.clear();
return this;
}
public NavigableMap build() {
return get();
}
public NavigableMap immutable() {
return Collections.unmodifiableNavigableMap(build());
}
public static , V> NavigableMapBuilder builder() {
return builder(Comparator.naturalOrder());
}
public static NavigableMapBuilder builder(Comparator super K> comparator) {
return new NavigableMapBuilder<>(comparator);
}
}
public static class EnumMapBuilder, V> extends GenericMapPopulator> {
public EnumMapBuilder(Class keyType) {
super(new EnumMap<>(Objects.requireNonNull(keyType, "No enum class specified")));
}
@Override
public EnumMapBuilder put(K k, V v) {
super.put(k, v);
return this;
}
@Override
public EnumMapBuilder remove(K k) {
super.remove(k);
return this;
}
@Override
public EnumMapBuilder putAll(Map extends K, ? extends V> other) {
super.putAll(other);
return this;
}
@Override
public EnumMapBuilder clear() {
super.clear();
return this;
}
public Map build() {
return get();
}
public Map immutable() {
return Collections.unmodifiableMap(build());
}
public static , V> EnumMapBuilder builder(Class keyType) {
return new EnumMapBuilder<>(keyType);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy