001/* 002 AUTOMATICALLY GENERATED BY jTemp FROM 003 /Users/jsh2/Work/openimaj/target/checkout/core/core/src/main/jtemp/org/openimaj/util/pair/Object#T#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.ArrayList; 037import java.util.Comparator; 038import java.util.List; 039 040import gnu.trove.list.array.TLongArrayList; 041 042/** 043 * A pair of a generic type <Q> and long 044 * 045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 046 * 047 * @param <Q> type of first object 048 */ 049public class ObjectLongPair<Q> { 050 /** 051 * Comparator for comparing the second element of a {@link ObjectLongPair} in ascending order. 052 */ 053 public static final Comparator<ObjectLongPair<?>> SECOND_ITEM_ASCENDING_COMPARATOR = new Comparator<ObjectLongPair<?>>() { 054 @Override 055 public int compare(ObjectLongPair<?> o1, ObjectLongPair<?> o2) { 056 if (o1.second < o2.second) 057 return -1; 058 if (o1.second > o2.second) 059 return 1; 060 return 0; 061 } 062 }; 063 064 /** 065 * Comparator for comparing the second element of a {@link ObjectLongPair} in descending order. 066 */ 067 public static final Comparator<ObjectLongPair<?>> SECOND_ITEM_DESCENDING_COMPARATOR = new Comparator<ObjectLongPair<?>>() { 068 @Override 069 public int compare(ObjectLongPair<?> o1, ObjectLongPair<?> o2) { 070 if (o1.second < o2.second) 071 return 1; 072 if (o1.second > o2.second) 073 return -1; 074 return 0; 075 } 076 }; 077 078 /** 079 * The first element of the pair 080 */ 081 public Q first; 082 083 /** 084 * The first element of the pair 085 */ 086 public long second; 087 088 /** 089 * Construct pair with given values 090 * 091 * @param f first value 092 * @param s second value 093 */ 094 public ObjectLongPair(Q f, long s) {first=f; second=s;} 095 096 /** 097 * Construct empty pair 098 */ 099 public ObjectLongPair() { } 100 101 /** 102 * Get the value of the first element 103 * @return the first value 104 */ 105 public Q getFirst() { 106 return first; 107 } 108 109 /** 110 * Set the value of the first element 111 * @param first the value to set 112 */ 113 public void setFirst(Q first) { 114 this.first = first; 115 } 116 117 /** 118 * Get the value of the second element 119 * @return the second 120 */ 121 public long getSecond() { 122 return second; 123 } 124 125 /** 126 * Set the value of the second element 127 * @param second the value to set 128 */ 129 public void setSecond(long second) { 130 this.second = second; 131 } 132 133 /** 134 * Create a pair from the given objects. 135 * 136 * @param <Q> Type of first object. 137 * @param q The first object. 138 * @param t The second object. 139 * @return The pair. 140 */ 141 public static <Q> ObjectLongPair<Q> pair(Q q, long t) { 142 return new ObjectLongPair<Q>(q, t); 143 } 144 145 /** 146 * Extract the second objects from a list of pairs. 147 * 148 * @param <Q> type of first object 149 * @param data the data 150 * @return extracted second objects 151 */ 152 public static <Q> TLongArrayList getSecond(Iterable<ObjectLongPair<Q>> data) { 153 TLongArrayList extracted = new TLongArrayList(); 154 155 for (ObjectLongPair<Q> item : data) 156 extracted.add(item.second); 157 158 return extracted; 159 } 160 161 /** 162 * Extract the first objects from a list of pairs. 163 * 164 * @param <Q> type of first object 165 * @param data the data 166 * @return extracted first objects 167 */ 168 public static <Q> List<Q> getFirst(Iterable<ObjectLongPair<Q>> data) { 169 List<Q> extracted = new ArrayList<Q>(); 170 171 for (ObjectLongPair<Q> item : data) 172 extracted.add(item.first); 173 174 return extracted; 175 } 176}