
org.smallmind.nutsnbolts.util.Tuple Maven / Gradle / Ivy
/*
* Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 David Berkman
*
* This file is part of the SmallMind Code Project.
*
* The SmallMind Code Project is free software, you can redistribute
* it and/or modify it under either, at your discretion...
*
* 1) The terms of GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* ...or...
*
* 2) The terms of the Apache License, Version 2.0.
*
* The SmallMind Code Project is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License or Apache License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* and the Apache License along with the SmallMind Code Project. If not, see
* or .
*
* Additional permission under the GNU Affero GPL version 3 section 7
* ------------------------------------------------------------------
* If you modify this Program, or any covered work, by linking or
* combining it with other code, such other code is not for that reason
* alone subject to any of the requirements of the GNU Affero GPL
* version 3.
*/
package org.smallmind.nutsnbolts.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Tuple implements Serializable, Cloneable {
private ArrayList keys;
private ArrayList values;
public Tuple () {
keys = new ArrayList();
values = new ArrayList();
}
public synchronized void clear () {
keys.clear();
values.clear();
}
public synchronized void add (Tuple tuple) {
for (int count = 0; count < tuple.size(); count++) {
addPair(tuple.getKey(count), tuple.getValue(count));
}
}
public synchronized void addPair (K key, V value) {
keys.add(key);
values.add(value);
}
public synchronized void addPair (int index, K key, V value) {
keys.add(index, key);
values.add(index, value);
}
public synchronized void setPair (K key, V value) {
int keyIndex;
if ((keyIndex = keys.indexOf(key)) < 0) {
addPair(key, value);
} else {
setValue(keyIndex, value);
}
}
public synchronized void setPair (int index, K key, V value) {
int keyIndex;
if ((keyIndex = keys.indexOf(key)) < 0) {
addPair(index, key, value);
} else {
setValue(keyIndex, value);
}
}
public synchronized void removeKey (K key) {
int index;
while ((index = keys.indexOf(key)) >= 0) {
removePair(index);
}
}
public synchronized V removePair (K key) {
int index;
if ((index = keys.indexOf(key)) >= 0) {
return removePair(index);
}
return null;
}
public synchronized V removePair (int index) {
keys.remove(index);
return values.remove(index);
}
public synchronized void setValue (int index, V value) {
values.set(index, value);
}
public synchronized void setValue (K key, V value) {
values.set(keys.indexOf(key), value);
}
public synchronized int size () {
return keys.size();
}
public synchronized K getKey (int index) {
return keys.get(index);
}
public synchronized List getKeys () {
return keys;
}
public synchronized Set getUniqueKeys () {
HashSet uniqueSet;
uniqueSet = new HashSet();
for (int count = 0; count < size(); count++) {
uniqueSet.add(keys.get(count));
}
return uniqueSet;
}
public synchronized int indexOfKey (K key) {
return keys.indexOf(key);
}
public synchronized V getValue (int index) {
return values.get(index);
}
public synchronized V getValue (K key) {
int index;
if ((index = keys.indexOf(key)) >= 0) {
return values.get(index);
}
return null;
}
public synchronized List getValues (K key) {
ArrayList allValues;
if (keys.indexOf(key) < 0) {
return null;
}
allValues = new ArrayList();
for (int count = 0; count < size(); count++) {
if ((keys.get(count)).equals(key)) {
allValues.add(values.get(count));
}
}
return allValues;
}
public synchronized boolean containsKey (K key) {
return keys.contains(key);
}
public synchronized boolean containsKeyValuePair (K key, V value) {
for (int count = 0; count < size(); count++) {
if ((keys.get(count)).equals(key)) {
if ((values.get(count)).equals(value)) {
return true;
}
}
}
return false;
}
public synchronized Map> asMap () {
Map> map = new HashMap<>();
for (int count = 0; count < size(); count++) {
List valueList;
if ((valueList = map.get(keys.get(count))) == null) {
map.put(keys.get(count), valueList = new LinkedList<>());
}
valueList.add(values.get(count));
}
return map;
}
public synchronized Object clone () {
Tuple tuple = new Tuple<>();
for (int count = 0; count < size(); count++) {
tuple.addPair(getKey(count), getValue(count));
}
return tuple;
}
public synchronized String toString () {
StringBuilder dataBuilder;
dataBuilder = new StringBuilder("[" + String.valueOf(size()) + "](");
for (int count = 0; count < size(); count++) {
if (count > 0) {
dataBuilder.append(";");
}
dataBuilder.append(getKey(count));
dataBuilder.append("=");
dataBuilder.append(getValue(count));
}
dataBuilder.append(")");
return dataBuilder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy