All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.d2ab.collection.Maps Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/*
 * Copyright 2016 Daniel Skogquist Åborg
 *
 * 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.d2ab.collection;

import org.d2ab.function.Functions;
import org.d2ab.function.QuaternaryFunction;

import java.util.*;
import java.util.Map.Entry;
import java.util.function.*;

import static java.util.Comparator.comparing;
import static org.d2ab.util.Comparators.naturalOrderNullsFirst;

/**
 * Utility methods for {@link Map} instances
 */
public class Maps {
	@SuppressWarnings("unchecked")
	private static final Comparator COMPARATOR =
			comparing((Function) Entry::getKey, naturalOrderNullsFirst()).thenComparing(
					(Function) Entry::getValue, naturalOrderNullsFirst());

	public static  Builder builder(IntFunction> constructor, int initialCapacity) {
		return new Builder<>(() -> constructor.apply(initialCapacity));
	}

	public static  Builder builder(K key, V value) {
		return Maps.builder().put(key, value);
	}

	public static  Builder builder() {
		return builder(HashMap::new);
	}

	public static  Builder builder(Supplier> constructor) {
		return new Builder<>(constructor);
	}

	/**
	 * Creates a new {@link Entry} with the given key and value. Calling {@link Entry#setValue(Object)} on the
	 * entry will result in an {@link UnsupportedOperationException} being thrown.
	 */
	public static  Entry entry(K key, V value) {
		return new EntryImpl<>(key, value);
	}

	public static  Map put(Map result, Entry entry) {
		result.put(entry.getKey(), entry.getValue());
		return result;
	}

	public static  UnaryOperator> asUnaryOperator(BiFunction> op) {
		return entry -> op.apply(entry.getKey(), entry.getValue());
	}

	public static  UnaryOperator> asUnaryOperator(
			BiFunction> f,
			BiFunction> g) {
		return Functions.toUnaryOperator(asFunction(f), asFunction(g));
	}

	public static  BinaryOperator> asBinaryOperator(QuaternaryFunction> f) {
		return (e1, e2) -> f.apply(e1.getKey(), e1.getValue(), e2.getKey(), e2.getValue());
	}

	public static  Function, R> asFunction(BiFunction mapper) {
		return entry -> mapper.apply(entry.getKey(), entry.getValue());
	}

	public static  Function, Entry> asFunction(
			Function keyMapper, Function valueMapper) {
		return entry -> entry(keyMapper.apply(entry.getKey()), valueMapper.apply(entry.getValue()));
	}

	public static  Predicate> asPredicate(BiPredicate predicate) {
		return entry -> predicate.test(entry.getKey(), entry.getValue());
	}

	public static  Consumer> asConsumer(BiConsumer action) {
		return entry -> action.accept(entry.getKey(), entry.getValue());
	}

	public static  boolean test(Entry entry, BiPredicate biPredicate) {
		return asPredicate(biPredicate).test(entry);
	}

	public static  Iterator iterator(Entry entry) {
		return new EntryIterator<>(entry);
	}

	public static class Builder {
		private Supplier> constructor;
		private Map map;

		private Builder(Supplier> constructor) {
			this.constructor = constructor;
		}

		public Builder put(K key, V value) {
			if (map == null)
				map = constructor.get();
			map.put(key, value);
			return this;
		}

		public Map build() {
			Map result = map;
			map = null;
			return result;
		}
	}

	private static class EntryImpl implements Entry, Comparable> {
		private final K key;
		private final V value;

		private EntryImpl(K key, V value) {
			this.key = key;
			this.value = value;
		}

		@Override
		public int hashCode() {
			int result = (key != null) ? key.hashCode() : 0;
			result = (31 * result) + ((value != null) ? value.hashCode() : 0);
			return result;
		}

		@Override
		public boolean equals(Object o) {
			if (o == this)
				return true;
			if (!(o instanceof Entry))
				return false;

			Entry that = (Entry) o;
			return Objects.equals(key, that.getKey()) && Objects.equals(value, that.getValue());
		}

		@Override
		public String toString() {
			return "<" + key + ", " + value + '>';
		}

		@Override
		public K getKey() {
			return key;
		}

		@Override
		public V getValue() {
			return value;
		}

		@Override
		public V setValue(V value) {
			throw new UnsupportedOperationException();
		}

		@Override
		public int compareTo(Entry that) {
			return COMPARATOR.compare(this, that);
		}
	}

	static class EntryIterator implements Iterator {
		private final Entry entry;
		int index;

		public EntryIterator(Entry entry) {
			this.entry = entry;
		}

		@Override
		public boolean hasNext() {
			return index < 2;
		}

		@Override
		public T next() {
			if (!hasNext())
				throw new NoSuchElementException();

			switch (++index) {
				case 1:
					return entry.getKey();
				case 2:
					return entry.getValue();
				default:
					// Can't happen due to above check
					throw new IllegalStateException();
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy