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

org.d2ab.util.Entries 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.util;

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

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

import static java.util.Comparator.*;

/**
 * Utility methods related to {@link Entry}.
 */
public class Entries {
	@SuppressWarnings("unchecked")
	private static final Comparator NULLS_FIRST = nullsFirst((Comparator) naturalOrder());

	private static final Function GET_KEY = (Function) Entry::getKey;
	private static final Function GET_VALUE = (Function) Entry::getValue;

	@SuppressWarnings("unchecked")
	private static final Comparator COMPARATOR =
			comparing(GET_KEY, NULLS_FIRST).thenComparing(GET_VALUE, NULLS_FIRST);

	private Entries() {
	}

	/**
	 * 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 of(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 -> of(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);
	}

	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 + '>';
		}

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

		@SuppressWarnings("NullableProblems")
		@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);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy