io.github.astrapi69.collections.pairs.KeyValuePair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of silly-beans Show documentation
Show all versions of silly-beans Show documentation
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.collections.pairs;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
/**
* The class {@link KeyValuePair} represents a key value pair with generic parameters for the key
* and value type.
*
*
* @param
* The generic type of the key
* @param
* The generic type of the value
*/
public final class KeyValuePair implements Serializable
{
/**
* The Constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* The key.
*/
private K key;
/**
* The value.
*/
private V value;
/**
* Instantiates a new key value pair.
*/
public KeyValuePair()
{
}
/**
* Instantiates a new key value pair.
*
* @param key
* the key
* @param value
* the value
*/
public KeyValuePair(final K key, final V value)
{
this.key = key;
this.value = value;
}
/**
* Builder.
*
* @param
* the key type
* @param
* the value type
* @return the key value pair builder
*/
public static KeyValuePairBuilder builder()
{
return new KeyValuePairBuilder<>();
}
/**
* Transforms the given {@link Map} to a list of {@link KeyValuePair}'s.
*
* @param
* The generic type of the key
* @param
* The generic type of the value
* @param map
* the map
* @return the new list with the {@link KeyValuePair}'s.
*/
public static List> toKeyValuePairs(final Map map)
{
final List> list = new ArrayList<>();
for (final Entry entry : map.entrySet())
{
list.add(
KeyValuePair. builder().key(entry.getKey()).value(entry.getValue()).build());
}
return list;
}
/**
* Transforms the given {@link Properties} to a list of {@link KeyValuePair}'s.
*
* @param properties
* the properties
* @return the new list with the {@link KeyValuePair}'s.
*/
public static List> toKeyValuePairs(final Properties properties)
{
final List> list = new ArrayList<>();
for (final Entry