001/* 002 AUTOMATICALLY GENERATED BY jTemp FROM 003 /Users/jsh2/Work/openimaj/target/checkout/core/core/src/main/jtemp/org/openimaj/util/pair/#A##B#Pair.jtemp 004*/ 005/** 006 * Copyright (c) 2011, The University of Southampton and the individual contributors. 007 * All rights reserved. 008 * 009 * Redistribution and use in source and binary forms, with or without modification, 010 * are permitted provided that the following conditions are met: 011 * 012 * * Redistributions of source code must retain the above copyright notice, 013 * this list of conditions and the following disclaimer. 014 * 015 * * Redistributions in binary form must reproduce the above copyright notice, 016 * this list of conditions and the following disclaimer in the documentation 017 * and/or other materials provided with the distribution. 018 * 019 * * Neither the name of the University of Southampton nor the names of its 020 * contributors may be used to endorse or promote products derived from this 021 * software without specific prior written permission. 022 * 023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 033 */ 034package org.openimaj.util.pair; 035 036import java.util.Comparator; 037 038/** 039 * A pair of long and short types 040 * 041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 042 * 043 */ 044public class LongShortPair { 045 /** 046 * Comparator for comparing the first element of a {@link LongShortPair} in ascending order. 047 */ 048 public static final Comparator<LongShortPair> FIRST_ITEM_ASCENDING_COMPARATOR = new Comparator<LongShortPair>() { 049 @Override 050 public int compare(LongShortPair o1, LongShortPair o2) { 051 if (o1.first < o2.first) 052 return -1; 053 if (o1.first > o2.first) 054 return 1; 055 return 0; 056 } 057 }; 058 059 /** 060 * Comparator for comparing the first element of a {@link LongShortPair} in descending order. 061 */ 062 public static final Comparator<LongShortPair> FIRST_ITEM_DESCENDING_COMPARATOR = new Comparator<LongShortPair>() { 063 @Override 064 public int compare(LongShortPair o1, LongShortPair o2) { 065 if (o1.first < o2.first) 066 return 1; 067 if (o1.first > o2.first) 068 return -1; 069 return 0; 070 } 071 }; 072 073 /** 074 * Comparator for comparing the second element of a {@link LongShortPair} in ascending order. 075 */ 076 public static final Comparator<LongShortPair> SECOND_ITEM_ASCENDING_COMPARATOR = new Comparator<LongShortPair>() { 077 @Override 078 public int compare(LongShortPair o1, LongShortPair o2) { 079 if (o1.second < o2.second) 080 return -1; 081 if (o1.second > o2.second) 082 return 1; 083 return 0; 084 } 085 }; 086 087 /** 088 * Comparator for comparing the second element of a {@link LongShortPair} in descending order. 089 */ 090 public static final Comparator<LongShortPair> SECOND_ITEM_DESCENDING_COMPARATOR = new Comparator<LongShortPair>() { 091 @Override 092 public int compare(LongShortPair o1, LongShortPair o2) { 093 if (o1.second < o2.second) 094 return 1; 095 if (o1.second > o2.second) 096 return -1; 097 return 0; 098 } 099 }; 100 101 /** 102 * The first element of the pair 103 */ 104 public long first; 105 106 /** 107 * The first element of the pair 108 */ 109 public short second; 110 111 /** 112 * Construct pair with given values 113 * 114 * @param f first value 115 * @param s second value 116 */ 117 public LongShortPair(long f, short s) {first=f; second=s;} 118 119 /** 120 * Construct empty pair 121 */ 122 public LongShortPair() {} 123 124 /** 125 * Get the value of the first element 126 * @return the first value 127 */ 128 public long getFirst() { 129 return first; 130 } 131 132 /** 133 * Set the value of the first element 134 * @param first the value to set 135 */ 136 public void setFirst(long first) { 137 this.first = first; 138 } 139 140 /** 141 * Get the value of the second element 142 * @return the second 143 */ 144 public short getSecond() { 145 return second; 146 } 147 148 /** 149 * Set the value of the second element 150 * @param second the value to set 151 */ 152 public void setSecond(short second) { 153 this.second = second; 154 } 155 156 /** 157 * Create a pair from the given values. 158 * 159 * @param a The first object. 160 * @param b The second object. 161 * @return The pair. 162 */ 163 public static LongShortPair pair(long a, short b) { 164 return new LongShortPair(a, b); 165 } 166 167 /** 168 * Extract the second values from a list of pairs. 169 * 170 * @param data the data 171 * @return extracted second values 172 */ 173 public static gnu.trove.list.array.TShortArrayList getSecond(Iterable<LongShortPair> data) { 174 gnu.trove.list.array.TShortArrayList extracted = new gnu.trove.list.array.TShortArrayList(); 175 176 for (LongShortPair item : data) 177 extracted.add(item.second); 178 179 return extracted; 180 } 181 182 /** 183 * Extract the first values from a list of pairs. 184 * 185 * @param data the data 186 * @return extracted first values 187 */ 188 public static gnu.trove.list.array.TLongArrayList getFirst(Iterable<LongShortPair> data) { 189 gnu.trove.list.array.TLongArrayList extracted = new gnu.trove.list.array.TLongArrayList(); 190 191 for (LongShortPair item : data) 192 extracted.add(item.first); 193 194 return extracted; 195 } 196 197 @Override 198 public String toString() { 199 return "("+first + ", "+ second+")"; 200 } 201}