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

io.github.astrapi69.collection.pair.KeyValuesPair Maven / Gradle / Ivy

Go to download

Utility library that provides some generic data beans like pair, triple and quattro

The newest version!
/**
 * The MIT License
 *
 * Copyright (C) 2015 Asterios Raptis
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package io.github.astrapi69.collection.pair;

import java.io.Serializable;
import java.util.Collection;

/**
 * The class {@link KeyValuesPair} represents a key value pair where the value is a collection with
 * generic parameters for the key and value type.
 *
 *
 * @param 
 *            The type of the key.
 * @param 
 *            The type of the values in the collection.
 */
public final class KeyValuesPair implements Serializable
{

	/**
	 * The Constant serialVersionUID.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * The key.
	 */
	private K key;
	/**
	 * The collection with the values.
	 */
	private Collection values;

	/**
	 * Instantiates a new key values pair.
	 */
	public KeyValuesPair()
	{
	}

	/**
	 * Instantiates a new key values pair.
	 *
	 * @param key
	 *            the key
	 * @param values
	 *            the values
	 */
	public KeyValuesPair(final K key, final Collection values)
	{
		this.key = key;
		this.values = values;
	}

	/**
	 * Builder.
	 *
	 * @param 
	 *            the key type
	 * @param 
	 *            the value type
	 * @return the key values pair builder
	 */
	public static  KeyValuesPairBuilder builder()
	{
		return new KeyValuesPairBuilder<>();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(final Object o)
	{
		if (o == this)
		{
			return true;
		}
		if (!(o instanceof KeyValuesPair))
		{
			return false;
		}
		final KeyValuesPair other = (KeyValuesPair)o;
		final Object this$key = this.getKey();
		final Object other$key = other.getKey();
		if (this$key == null ? other$key != null : !this$key.equals(other$key))
		{
			return false;
		}
		final Object this$values = this.getValues();
		final Object other$values = other.getValues();
		if (this$values == null ? other$values != null : !this$values.equals(other$values))
		{
			return false;
		}
		return true;
	}

	/**
	 * The key.
	 *
	 * @return the key
	 */
	public K getKey()
	{
		return this.key;
	}

	/**
	 * The key.
	 *
	 * @param key
	 *            the new key
	 */
	public void setKey(final K key)
	{
		this.key = key;
	}

	/**
	 * The collection with the values.
	 *
	 * @return the values
	 */
	public Collection getValues()
	{
		return this.values;
	}

	/**
	 * The collection with the values.
	 *
	 * @param values
	 *            the new values
	 */
	public void setValues(final Collection values)
	{
		this.values = values;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode()
	{
		final int PRIME = 59;
		int result = 1;
		final Object $key = this.getKey();
		result = result * PRIME + ($key == null ? 43 : $key.hashCode());
		final Object $values = this.getValues();
		result = result * PRIME + ($values == null ? 43 : $values.hashCode());
		return result;
	}

	/**
	 * To builder.
	 *
	 * @return the key values pair builder
	 */
	public KeyValuesPairBuilder toBuilder()
	{
		final KeyValuesPairBuilder builder = new KeyValuesPairBuilder().key(this.key);
		if (this.values != null)
		{
			builder.values(this.values);
		}
		return builder;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString()
	{
		return "KeyValuesPair(key=" + this.getKey() + ", values=" + this.getValues() + ")";
	}

	/**
	 * The class {@link KeyValuesPairBuilder}.
	 *
	 * @param 
	 *            the key type
	 * @param 
	 *            the value type
	 */
	public static class KeyValuesPairBuilder
	{

		/** The key. */
		private K key;

		/** The values. */
		private java.util.ArrayList values;


		/**
		 * Instantiates a new key values pair builder.
		 */
		KeyValuesPairBuilder()
		{
		}

		/**
		 * Build it
		 *
		 * @return the key values pair
		 */
		public KeyValuesPair build()
		{
			java.util.Collection values;
			switch (this.values == null ? 0 : this.values.size())
			{
				case 0 :
					values = java.util.Collections.emptyList();
					break;
				case 1 :
					values = java.util.Collections.singletonList(this.values.get(0));
					break;
				default :
					values = java.util.Collections
						.unmodifiableList(new java.util.ArrayList<>(this.values));
			}
			return new KeyValuesPair<>(key, values);
		}

		/**
		 * Clear values.
		 *
		 * @return the key values pair builder
		 */
		public KeyValuesPairBuilder clearValues()
		{
			if (this.values != null)
			{
				this.values.clear();
			}
			return this;
		}

		/**
		 * The key.
		 *
		 * @param key
		 *            the key
		 * @return the key values pair builder
		 */
		public KeyValuesPairBuilder key(final K key)
		{
			this.key = key;
			return this;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String toString()
		{
			return "KeyValuesPair.KeyValuesPairBuilder(key=" + this.key + ", values=" + this.values
				+ ")";
		}

		/**
		 * Value.
		 *
		 * @param value
		 *            the value
		 * @return the key values pair builder
		 */
		public KeyValuesPairBuilder value(final V value)
		{
			if (this.values == null)
			{
				this.values = new java.util.ArrayList<>();
			}
			this.values.add(value);
			return this;
		}

		/**
		 * Values.
		 *
		 * @param values
		 *            the values
		 * @return the key values pair builder
		 */
		public KeyValuesPairBuilder values(final java.util.Collection values)
		{
			if (this.values == null)
			{
				this.values = new java.util.ArrayList<>();
			}
			this.values.addAll(values);
			return this;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy