
org.apache.dubbo.common.utils.Pair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dubbo Show documentation
Show all versions of dubbo Show documentation
The all in one project of dubbo
/*
* 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 org.apache.dubbo.common.utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class Pair implements Map.Entry, Comparable>, Serializable {
private static final long serialVersionUID = 1L;
@SuppressWarnings("rawtypes")
private static final Pair NULL = new Pair<>(null, null);
public final L left;
public final R right;
public static Pair of(L left, R right) {
return left == null && right == null ? nullPair() : new Pair<>(left, right);
}
@SuppressWarnings("unchecked")
public static Pair nullPair() {
return NULL;
}
@SafeVarargs
public static Map toMap(Pair... pairs) {
if (pairs == null) {
return Collections.emptyMap();
}
return toMap(Arrays.asList(pairs));
}
public static Map toMap(Collection> pairs) {
if (pairs == null) {
return Collections.emptyMap();
}
Map map = CollectionUtils.newLinkedHashMap(pairs.size());
for (Pair pair : pairs) {
map.put(pair.getLeft(), pair.getRight());
}
return map;
}
public static List> toPairs(Map map) {
if (map == null) {
return Collections.emptyList();
}
List> pairs = new ArrayList<>(map.size());
for (Map.Entry entry : map.entrySet()) {
pairs.add(of(entry.getKey(), entry.getValue()));
}
return pairs;
}
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
public boolean isNull() {
return this == NULL || left == null && right == null;
}
@Override
public L getKey() {
return left;
}
@Override
public R getValue() {
return right;
}
@Override
public R setValue(R value) {
throw new UnsupportedOperationException();
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(Pair other) {
return left.equals(other.left)
? ((Comparable) right).compareTo(other.right)
: ((Comparable) left).compareTo(other.left);
}
@Override
public int hashCode() {
return Objects.hashCode(left) ^ Objects.hashCode(right);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof Map.Entry) {
Map.Entry, ?> that = (Map.Entry, ?>) other;
return Objects.equals(left, that.getKey()) && Objects.equals(right, that.getValue());
}
return false;
}
@Override
public String toString() {
return "(" + left + ", " + right + ')';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy