001/*
002 * BridJ - Dynamic and blazing-fast native interop for Java.
003 * http://bridj.googlecode.com/
004 *
005 * Copyright (c) 2010-2013, Olivier Chafik (http://ochafik.com/)
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions are met:
010 * 
011 *     * Redistributions of source code must retain the above copyright
012 *       notice, this list of conditions and the following disclaimer.
013 *     * Redistributions in binary form must reproduce the above copyright
014 *       notice, this list of conditions and the following disclaimer in the
015 *       documentation and/or other materials provided with the distribution.
016 *     * Neither the name of Olivier Chafik nor the
017 *       names of its contributors may be used to endorse or promote products
018 *       derived from this software without specific prior written permission.
019 * 
020 * THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
021 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
023 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
024 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
027 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 */
031package org.bridj.util;
032
033import java.util.Map;
034
035public class Pair<U, V> implements Comparable<Pair<U, V>>, Map.Entry<U, V> {
036
037    private U first;
038    private V second;
039
040    public Pair(U first, V second) {
041        this.first = first;
042        this.second = second;
043    }
044
045    public Pair() {
046    }
047
048    public U getFirst() {
049        return first;
050    }
051
052    public V getSecond() {
053        return second;
054    }
055
056    public void setFirst(U first) {
057        this.first = first;
058    }
059
060    public void setSecond(V second) {
061        this.second = second;
062    }
063
064    @SuppressWarnings("unchecked")
065    public int compareTo(Pair<U, V> o) {
066        Comparable<U> cu = (Comparable<U>) getFirst();
067        if (cu == null) {
068            if (first != null) {
069                return 1;
070            }
071        } else {
072            int d = cu.compareTo(o.getFirst());
073            if (d != 0) {
074                return d;
075            }
076        }
077
078        Comparable<V> cv = (Comparable<V>) getSecond();
079        if (cv == null) {
080            return second != null ? 1 : -1;
081        }
082        return cv.compareTo(o.getSecond());
083    }
084
085    @Override
086    public String toString() {
087        return "Pair(" + first + ", " + second + ")";
088    }
089
090    public U getKey() {
091        return first;
092    }
093
094    public V getValue() {
095        return second;
096    }
097
098    public V setValue(V value) {
099        V oldValue = second;
100        second = value;
101        return oldValue;
102    }
103
104    @Override
105    public int hashCode() {
106        final int prime = 31;
107        int result = 1;
108        result = prime * result + ((first == null) ? 0 : first.hashCode());
109        result = prime * result + ((second == null) ? 0 : second.hashCode());
110        return result;
111    }
112
113    @Override
114    public boolean equals(Object obj) {
115        if (this == obj) {
116            return true;
117        }
118        if (obj == null) {
119            return false;
120        }
121        if (getClass() != obj.getClass()) {
122            return false;
123        }
124        final Pair<?, ?> other = (Pair<?, ?>) obj;
125        if (first == null) {
126            if (other.first != null) {
127                return false;
128            }
129        } else if (!first.equals(other.first)) {
130            return false;
131        }
132        if (second == null) {
133            if (other.second != null) {
134                return false;
135            }
136        } else if (!second.equals(other.second)) {
137            return false;
138        }
139        return true;
140    }
141
142    public boolean isFull() {
143        return getFirst() != null && getSecond() != null;
144    }
145
146    public boolean isEmpty() {
147        return getFirst() == null && getSecond() == null;
148    }
149}