List of usage examples for java.lang Integer compareTo
public int compareTo(Integer anotherInteger)
From source file:org.omnaest.utils.structure.iterator.IterableUtils.java
/** * Returns a {@link Map} containing the {@link Set} of elements as keys and the count of each element as value.<br> * <br>//from w w w . j a va 2 s. c om * E.g. [a,b,a,a] will be returned as [a=3,b=1] * * @param iterable * @return {@link Map} */ public static <E> Map<E, Integer> toCountedElementsMap(Iterable<E> iterable) { class ComparatorUsingMap implements Comparator<E> { private final Map<E, Integer> map; private final List<E> list; @Override public int compare(E o1, E o2) { final Integer value1 = this.map.get(o1); final Integer value2 = this.map.get(o2); final int compareTo = -1 * value1.compareTo(value2); int retval = compareTo; if (retval == 0) { retval = Integer.valueOf(this.list.indexOf(o1)).compareTo(this.list.indexOf(o2)); } return retval; } public ComparatorUsingMap(Map<E, Integer> map, List<E> list) { super(); this.map = map; this.list = list; } } final Map<E, Integer> retmap = new HashMap<E, Integer>(); final List<E> list = new ArrayList<E>(); if (iterable != null) { for (E element : iterable) { Integer count = retmap.get(element); if (count == null) { count = 0; retmap.put(element, count); } retmap.put(element, ++count); list.add(element); } } final ComparatorUsingMap comparator = new ComparatorUsingMap(retmap, list); final Map<E, Integer> retmapSorted = new TreeMap<E, Integer>(comparator); retmapSorted.putAll(retmap); return new LinkedHashMap<E, Integer>(retmapSorted); }
From source file:ips1ap101.lib.core.util.STP.java
public static boolean esObjetoEnRango(Object objeto, Object minimo, Object maximo) { boolean es = true; TipoDatoParEnumeration tipo;/*from w w w. jav a 2 s . c o m*/ if (objeto == null) { return false; } else if (objeto instanceof String) { tipo = TipoDatoParEnumeration.ALFANUMERICO; } else if (objeto instanceof BigDecimal) { tipo = TipoDatoParEnumeration.NUMERICO; } else if (objeto instanceof Timestamp) { tipo = TipoDatoParEnumeration.FECHA_HORA; } else if (objeto instanceof Integer) { tipo = TipoDatoParEnumeration.ENTERO; } else if (objeto instanceof Long) { tipo = TipoDatoParEnumeration.ENTERO_GRANDE; } else if (objeto instanceof BigInteger) { tipo = TipoDatoParEnumeration.ENTERO_GRANDE; } else { return false; } switch (tipo) { case ALFANUMERICO: String val1 = (String) objeto; String min1 = (String) minimo; String max1 = (String) maximo; if (min1 != null && val1.compareTo(min1) < 0) { es = false; } if (max1 != null && val1.compareTo(max1) > 0) { es = false; } break; case NUMERICO: BigDecimal val2 = (BigDecimal) objeto; BigDecimal min2 = (BigDecimal) minimo; BigDecimal max2 = (BigDecimal) maximo; if (min2 != null && val2.compareTo(min2) < 0) { es = false; } if (max2 != null && val2.compareTo(max2) > 0) { es = false; } break; case FECHA_HORA: Timestamp val3 = (Timestamp) objeto; Timestamp min3 = (Timestamp) minimo; Timestamp max3 = (Timestamp) maximo; if (min3 != null && val3.compareTo(min3) < 0) { es = false; } if (max3 != null && val3.compareTo(max3) > 0) { es = false; } break; case ENTERO: Integer val4 = (Integer) objeto; Integer min4 = (Integer) minimo; Integer max4 = (Integer) maximo; if (min4 != null && val4.compareTo(min4) < 0) { es = false; } if (max4 != null && val4.compareTo(max4) > 0) { es = false; } break; case ENTERO_GRANDE: Long val5 = objeto instanceof BigInteger ? ((BigInteger) objeto).longValue() : (Long) objeto; Long min5 = (Long) minimo; Long max5 = (Long) maximo; if (min5 != null && val5.compareTo(min5) < 0) { es = false; } if (max5 != null && val5.compareTo(max5) > 0) { es = false; } break; } return es; }
From source file:com.egt.core.util.STP.java
public static boolean esObjetoEnRango(Object objeto, Object minimo, Object maximo) { boolean es = true; EnumTipoDatoPar tipo;/*ww w . j a va2 s .c o m*/ if (objeto == null) { return false; } else if (objeto instanceof String) { tipo = EnumTipoDatoPar.ALFANUMERICO; } else if (objeto instanceof BigDecimal) { tipo = EnumTipoDatoPar.NUMERICO; } else if (objeto instanceof Timestamp) { tipo = EnumTipoDatoPar.FECHA_HORA; } else if (objeto instanceof Integer) { tipo = EnumTipoDatoPar.ENTERO; } else if (objeto instanceof Long) { tipo = EnumTipoDatoPar.ENTERO_GRANDE; } else if (objeto instanceof BigInteger) { tipo = EnumTipoDatoPar.ENTERO_GRANDE; } else { return false; } switch (tipo) { case ALFANUMERICO: String val1 = (String) objeto; String min1 = (String) minimo; String max1 = (String) maximo; if (min1 != null && val1.compareTo(min1) < 0) { es = false; } if (max1 != null && val1.compareTo(max1) > 0) { es = false; } break; case NUMERICO: BigDecimal val2 = (BigDecimal) objeto; BigDecimal min2 = (BigDecimal) minimo; BigDecimal max2 = (BigDecimal) maximo; if (min2 != null && val2.compareTo(min2) < 0) { es = false; } if (max2 != null && val2.compareTo(max2) > 0) { es = false; } break; case FECHA_HORA: Timestamp val3 = (Timestamp) objeto; Timestamp min3 = (Timestamp) minimo; Timestamp max3 = (Timestamp) maximo; if (min3 != null && val3.compareTo(min3) < 0) { es = false; } if (max3 != null && val3.compareTo(max3) > 0) { es = false; } break; case ENTERO: Integer val4 = (Integer) objeto; Integer min4 = (Integer) minimo; Integer max4 = (Integer) maximo; if (min4 != null && val4.compareTo(min4) < 0) { es = false; } if (max4 != null && val4.compareTo(max4) > 0) { es = false; } break; case ENTERO_GRANDE: Long val5 = objeto instanceof BigInteger ? ((BigInteger) objeto).longValue() : (Long) objeto; Long min5 = (Long) minimo; Long max5 = (Long) maximo; if (min5 != null && val5.compareTo(min5) < 0) { es = false; } if (max5 != null && val5.compareTo(max5) > 0) { es = false; } break; } return es; }
From source file:org.ut.biolab.medsavant.shared.vcf.VariantRecord.java
public static int compareChrom(String chrom1, String chrom2) { chrom1 = chrom1.substring(3);// w w w . j a v a 2 s . co m chrom2 = chrom2.substring(3); try { if (NumberUtils.isNumber(chrom1) && NumberUtils.isNumber(chrom2)) { Integer a = Integer.parseInt(chrom1); Integer b = Integer.parseInt(chrom2); return a.compareTo(b); } } catch (NumberFormatException e) { //return chrom1.compareTo(chrom2); } return chrom1.compareTo(chrom2); }
From source file:com.att.aro.core.util.Util.java
public static Comparator<Integer> getDomainIntSorter() { intComparator = new Comparator<Integer>() { @Override/*from w w w . ja v a 2 s . co m*/ public int compare(Integer a, Integer b) { if (a.compareTo(b) > 0) return -1; else if (a.compareTo(b) < 0) return 1; else return 0; } }; return intComparator; }
From source file:org.pentaho.reporting.libraries.base.util.ObjectUtilities.java
/** * Compares version numbers.//from w w w. j a v a 2 s.c o m * * @param a1 the first array. * @param a2 the second array. * @return -1 if a1 is less than a2, 0 if a1 and a2 are equal, and +1 otherwise. */ public static int compareVersionArrays(final Integer[] a1, final Integer[] a2) { final int length = Math.min(a1.length, a2.length); for (int i = 0; i < length; i++) { final Integer o1 = a1[i]; final Integer o2 = a2[i]; if (o1 == null && o2 == null) { // cannot decide .. continue; } if (o1 == null) { return 1; } if (o2 == null) { return -1; } final int retval = o1.compareTo(o2); if (retval != 0) { return retval; } } return 0; }
From source file:com.borqs.sync.server.contact.MergeUtils.java
/** * *//*from w w w.j a v a2 s.c om*/ public static MergeResult compareIntegers(Integer integerA, Integer integerB) { MergeResult result = null; if (integerA == null && integerB == null) { return new MergeResult(false, false); } if (integerA != null && integerB == null) { result = new MergeResult(); result.addPropertyB("Integer", integerA.intValue() + ", null"); return result; } if (integerA == null && integerB != null) { result = new MergeResult(); result.addPropertyA("Integer", "null, " + integerB.intValue()); return result; } if (integerA.compareTo(integerB) != 0) { // // The values are different so // an update on integerB is required // result = new MergeResult(); result.addPropertyB("Integer", integerA.intValue() + ", " + integerB.intValue()); return result; } return new MergeResult(false, false); }
From source file:controllers.nwbib.Application.java
/** * @param q Query to search in all fields * @param person Query for a person associated with the resource * @param name Query for the resource name (title) * @param subject Query for the resource subject * @param id Query for the resource id// ww w . j av a 2 s .c om * @param publisher Query for the resource publisher * @param issued Query for the resource issued year * @param medium Query for the resource medium * @param nwbibspatial Query for the resource nwbibspatial classification * @param nwbibsubject Query for the resource nwbibsubject classification * @param from The page start (offset of page of resource to return) * @param size The page size (size of page of resource to return) * @param owner Owner filter for resource queries * @param t Type filter for resource queries * @param field The facet field (the field to facet over) * @param sort Sorting order for results ("newest", "oldest", "" -> relevance) * @param set The set, overrides the default NWBib set if not empty * @param location A polygon describing the subject area of the resources * @param word A word, a concept from the hbz union catalog * @param corporation A corporation associated with the resource * @param raw A query string that's directly (unprocessed) passed to ES * @return The search results */ public static Promise<Result> facets(String q, String person, String name, String subject, String id, String publisher, String issued, String medium, String nwbibspatial, String nwbibsubject, int from, int size, String owner, String t, String field, String sort, String set, String location, String word, String corporation, String raw) { String key = String.format("facets.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, q, person, name, id, publisher, set, location, word, corporation, raw, subject, issued, medium, nwbibspatial, nwbibsubject, owner, t); Result cachedResult = (Result) Cache.get(key); if (cachedResult != null) { return Promise.promise(() -> cachedResult); } String labelTemplate = "<span class='%s'/> %s (%s)"; Function<JsonNode, Pair<JsonNode, String>> toLabel = json -> { String term = json.get("term").asText(); int count = json.get("count").asInt(); String icon = Lobid.facetIcon(Arrays.asList(term), field); String label = Lobid.facetLabel(Arrays.asList(term), field, ""); String fullLabel = String.format(labelTemplate, icon, label, count); return Pair.of(json, fullLabel); }; Predicate<Pair<JsonNode, String>> labelled = pair -> { JsonNode json = pair.getLeft(); String label = pair.getRight(); int count = json.get("count").asInt(); return (!label.contains("http") || label.contains("nwbib")) && label.length() > String.format(labelTemplate, "", "", count).length(); }; Collator collator = Collator.getInstance(Locale.GERMAN); Comparator<Pair<JsonNode, String>> sorter = (p1, p2) -> { String t1 = p1.getLeft().get("term").asText(); String t2 = p2.getLeft().get("term").asText(); boolean t1Current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, t1, raw); boolean t2Current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, t2, raw); if (t1Current == t2Current) { if (!field.equals(ISSUED_FIELD)) { Integer c1 = p1.getLeft().get("count").asInt(); Integer c2 = p2.getLeft().get("count").asInt(); return c2.compareTo(c1); } String l1 = p1.getRight().substring(p1.getRight().lastIndexOf('>') + 1); String l2 = p2.getRight().substring(p2.getRight().lastIndexOf('>') + 1); return collator.compare(l1, l2); } return t1Current ? -1 : t2Current ? 1 : 0; }; Function<Pair<JsonNode, String>, String> toHtml = pair -> { JsonNode json = pair.getLeft(); String fullLabel = pair.getRight(); String term = json.get("term").asText(); if (field.equals(SUBJECT_LOCATION_FIELD)) { GeoPoint point = new GeoPoint(term); term = String.format("%s,%s", point.getLat(), point.getLon()); } String mediumQuery = !field.equals(MEDIUM_FIELD) // ? medium : queryParam(medium, term); String typeQuery = !field.equals(TYPE_FIELD) // ? t : queryParam(t, term); String ownerQuery = !field.equals(ITEM_FIELD) // ? owner : withoutAndOperator(queryParam(owner, term)); String nwbibsubjectQuery = !field.equals(NWBIB_SUBJECT_FIELD) // ? nwbibsubject : queryParam(nwbibsubject, term); String nwbibspatialQuery = !field.equals(NWBIB_SPATIAL_FIELD) // ? nwbibspatial : queryParam(nwbibspatial, term); String rawQuery = !field.equals(COVERAGE_FIELD) // ? raw : rawQueryParam(raw, term); String locationQuery = !field.equals(SUBJECT_LOCATION_FIELD) // ? location : term; String subjectQuery = !field.equals(SUBJECT_FIELD) // ? subject : queryParam(subject, term); String issuedQuery = !field.equals(ISSUED_FIELD) // ? issued : queryParam(issued, term); boolean current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, term, raw); String routeUrl = routes.Application.search(q, person, name, subjectQuery, id, publisher, issuedQuery, mediumQuery, nwbibspatialQuery, nwbibsubjectQuery, from, size, ownerQuery, typeQuery, sort(sort, nwbibspatialQuery, nwbibsubjectQuery, subjectQuery), false, set, locationQuery, word, corporation, rawQuery).url(); String result = String.format( "<li " + (current ? "class=\"active\"" : "") + "><a class=\"%s-facet-link\" href='%s'>" + "<input onclick=\"location.href='%s'\" class=\"facet-checkbox\" " + "type=\"checkbox\" %s> %s</input>" + "</a></li>", Math.abs(field.hashCode()), routeUrl, routeUrl, current ? "checked" : "", fullLabel); return result; }; Promise<Result> promise = Lobid.getFacets(q, person, name, subject, id, publisher, issued, medium, nwbibspatial, nwbibsubject, owner, field, t, set, location, word, corporation, raw).map(json -> { Stream<JsonNode> stream = StreamSupport.stream( Spliterators.spliteratorUnknownSize(json.findValue("entries").elements(), 0), false); if (field.equals(ITEM_FIELD)) { stream = preprocess(stream); } String labelKey = String.format( "facets-labels.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, raw, q, person, name, id, publisher, set, word, corporation, subject, issued, medium, nwbibspatial, nwbibsubject, raw, field.equals(ITEM_FIELD) ? "" : owner, t, location); @SuppressWarnings("unchecked") List<Pair<JsonNode, String>> labelledFacets = (List<Pair<JsonNode, String>>) Cache .get(labelKey); if (labelledFacets == null) { labelledFacets = stream.map(toLabel).filter(labelled).collect(Collectors.toList()); Cache.set(labelKey, labelledFacets, ONE_DAY); } return labelledFacets.stream().sorted(sorter).map(toHtml).collect(Collectors.toList()); }).map(lis -> ok(String.join("\n", lis))); promise.onRedeem(r -> Cache.set(key, r, ONE_DAY)); return promise; }
From source file:Main.java
@Override public int compare(Integer first, Integer second) { return second.compareTo(first); }
From source file:org.openmrs.module.rwandaprimarycare.PrimaryCareBusinessLogic.java
/** * * Sorts patients by location//from ww w . j a va 2 s.com * * @param patientList * @param userLocation * @return */ public static List<Patient> sortResultsForUser(List<Patient> patientList, final Location userLocation) { if (userLocation != null) { Collections.sort(patientList, new Comparator<Patient>() { public int compare(Patient left, Patient right) { Integer leftWeight = getLocations(left).contains(userLocation) ? 0 : 1; Integer rightWeight = getLocations(right).contains(userLocation) ? 0 : 1; return leftWeight.compareTo(rightWeight); } }); } return patientList; }