alexh.Fluent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent Show documentation
Show all versions of fluent Show documentation
Java 8 common library for fluent coding
The newest version!
/*
* Copyright 2015 Alex Butler
*
* 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 alexh;
import static java.util.Collections.unmodifiableMap;
import java.util.Comparator;
/**
* Container for fluent class implementation that allows declarative object building style.
* Usage:
* {@code
* HashMap map = new Fluent.HashMap()
* .append("key1", "val1")
* .append("key2", "val2");
* }
*
* @author Alex Butler
*/
public class Fluent {
/** Fluent extension of java.util.Map */
public interface Map extends java.util.Map {
/**
* @see java.util.Map#put(K, V)
* @return self-reference
*/
default Map append(K key, V val) {
put(key, val);
return this;
}
/**
* @see java.util.Map#putAll(java.util.Map)
* @return self-reference
*/
default Map appendAll(java.util.Map extends K, ? extends V> map) {
putAll(map);
return this;
}
/**
* Equivalent to append(key, value) of the input entry
* @see #append(K, V)
* @param entry map entry
* @return self-reference
*/
default Map append(Map.Entry extends K, ? extends V> entry) {
return append(entry.getKey(), entry.getValue());
}
/**
* Returns an unmodifiable view of this map. Best used at the end of fluent construction
* thereby providing an effectively immutable map (as the backing mutable map is unreachable).
* For example:
* {@code
* Map immutable = new Fluent.HashMap()
* .append("one", 1)
* .append("two", 2)
* .append("three", 3)
* .unmodifiable();
* }
* See java.util.Collections#unmodifiableMap(Map)
* @return an unmodifiable view of this map
*/
default java.util.Map unmodifiable() {
return unmodifiableMap(this);
}
}
public static class HashMap extends java.util.HashMap implements Fluent.Map {
public HashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public HashMap(int initialCapacity) {
super(initialCapacity);
}
public HashMap(java.util.Map m) {
super(m);
}
public HashMap() {}
}
public static class LinkedHashMap extends java.util.LinkedHashMap implements Fluent.Map {
public LinkedHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public LinkedHashMap(int initialCapacity) {
super(initialCapacity);
}
public LinkedHashMap(java.util.Map m) {
super(m);
}
public LinkedHashMap() {}
}
public static class IdentityHashMap extends java.util.IdentityHashMap implements Fluent.Map {
public IdentityHashMap() {}
public IdentityHashMap(int expectedMaxSize) {
super(expectedMaxSize);
}
public IdentityHashMap(java.util.Map extends K, ? extends V> m) {
super(m);
}
}
public static class EnumMap, V> extends java.util.EnumMap implements Fluent.Map {
public EnumMap(Class keyType) {
super(keyType);
}
public EnumMap(java.util.EnumMap m) {
super(m);
}
public EnumMap(java.util.Map m) {
super(m);
}
}
public static class WeakHashMap extends java.util.WeakHashMap implements Fluent.Map {
public WeakHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public WeakHashMap(int initialCapacity) {
super(initialCapacity);
}
public WeakHashMap() {}
public WeakHashMap(java.util.Map extends K, ? extends V> m) {
super(m);
}
}
public static class ConcurrentSkipListMap extends java.util.concurrent.ConcurrentSkipListMap implements Fluent.Map {
public ConcurrentSkipListMap() {}
public ConcurrentSkipListMap(Comparator super K> comparator) {
super(comparator);
}
public ConcurrentSkipListMap(java.util.Map extends K, ? extends V> m) {
super(m);
}
public ConcurrentSkipListMap(java.util.SortedMap m) {
super(m);
}
}
public static class ConcurrentHashMap extends java.util.concurrent.ConcurrentHashMap implements Fluent.Map {
public ConcurrentHashMap() {}
public ConcurrentHashMap(int initialCapacity) {
super(initialCapacity);
}
public ConcurrentHashMap(java.util.Map extends K, ? extends V> m) {
super(m);
}
public ConcurrentHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
super(initialCapacity, loadFactor, concurrencyLevel);
}
}
private Fluent() {}
}