com.davidbracewell.tuple.Tuple2 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
A set of utilities and tools to speed up and ease programming in Java.
/*
* (c) 2005 David B. Bracewell
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.davidbracewell.tuple;
import com.davidbracewell.collection.Sorting;
import lombok.Data;
import java.io.Serializable;
import java.util.Map;
/**
* The type Tuple 2.
*
* @param the type parameter
* @param the type parameter
* @author David B. Bracewell
*/
@Data
public class Tuple2 implements Serializable, Map.Entry, Comparable>, Tuple {
private static final long serialVersionUID = 1L;
/**
* The first value
*/
public final K v1;
/**
* The second value
*/
public final V v2;
/**
* Instantiates a new Tuple 2.
*
* @param v1 the first value
* @param v2 the second value
*/
public Tuple2(K v1, V v2) {
this.v1 = v1;
this.v2 = v2;
}
@Override
public int degree() {
return 2;
}
@Override
public Object[] array() {
return new Object[]{v1, v2};
}
@Override
public int compareTo(Tuple2 o) {
if (o == null) {
return 1;
}
int result = Sorting.compare(v1, o.v1);
return result != 0 ? result : Sorting.compare(v2, o.v2);
}
@Override
public K getKey() {
return v1;
}
@Override
public V getValue() {
return v2;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
/**
* Of tuple 2.
*
* @param the type parameter
* @param the type parameter
* @param key the key
* @param value the value
* @return the tuple 2
*/
public static Tuple2 of(K key, V value) {
return new Tuple2<>(key, value);
}
/**
* Expand tuple 3.
*
* @param the type parameter
* @param z the z
* @return the tuple 3
*/
public Tuple3 append(Z z) {
return Tuple3.of(v1, v2, z);
}
/**
* Append tuple 4.
*
* @param the type parameter
* @param the type parameter
* @param other the other
* @return the tuple 4
*/
public Tuple4 append(Tuple2 other) {
if (other == null) {
return Tuple4.of(v1, v2, null, null);
}
return Tuple4.of(v1, v2, other.v1, other.v2);
}
@Override
public Tuple3 appendLeft(T object) {
return Tuple3.of(object, v1, v2);
}
@Override
public Tuple3 appendRight(T object) {
return Tuple3.of(v1, v2, object);
}
@Override
public Tuple1 shiftLeft() {
return Tuple1.of(v2);
}
@Override
public Tuple1 shiftRight() {
return Tuple1.of(v1);
}
@Override
public String toString() {
return "(" + v1 + ", " + v2 + ")";
}
}//END OF Tuple2
© 2015 - 2025 Weber Informatics LLC | Privacy Policy