List of usage examples for java.util Comparator Comparator
Comparator
From source file:de.uni_leipzig.simba.memorymanagement.Index.graphclustering.EdgeGreedyClustering.java
@Override public Map<Integer, Cluster> cluster(Graph g, int maxSize) { Comparator<Edge> comparator = new Comparator<Edge>() { public int compare(Edge e1, Edge e2) { return e1.getWeight() - e2.getWeight(); // use your logic }/*w ww. j a va 2s . c o m*/ }; //init Set<Edge> availableEdges = new HashSet<>(); int count = 0; for (Edge e : g.getAllEdges()) { //System.out.println("<" + e.source.item.getId() + ">" + " => <" + e.target.item.getId() + ">"); //if (!(e.source.item.getId().equals(e.target.item.getId()))) { availableEdges.add(e); //} } Map<Integer, Cluster> result = new HashMap<>(); Cluster cluster; while (!availableEdges.isEmpty()) { // System.out.println("Available edges:" + availableEdges); int clusterSize; Set<Edge> emanatingEdges = new HashSet<>(); //get largest available edge and start cluster List<Edge> sortedEdges = new ArrayList<>(availableEdges); //get largest available edge and start cluster Collections.sort(sortedEdges, comparator); Edge e = sortedEdges.get(0); cluster = new Cluster(count); result.put(count, cluster); count++; cluster.nodes.add(e.source); cluster.nodes.add(e.target); cluster.edges.add(e); //itemClusterMap.get(e.source.toString()).add(clusters.size()); //itemClusterMap.get(e.target.toString()).add(clusters.size()); clusterSize = e.source.getWeight() + e.target.getWeight(); if (clusterSize > maxSize) { System.err.println("Basic condition for scheduling broken. Updating max size to " + clusterSize); maxSize = clusterSize; } availableEdges.remove(e); //take all edges between two nodes in the cluster and add them to the //edges of the cluster Set<Edge> toRemove = new HashSet<>(); for (Edge e2 : availableEdges) { if (cluster.nodes.contains(e2.source) && cluster.nodes.contains(e2.target)) { cluster.edges.add(e2); toRemove.add(e2); } } for (Edge e2 : toRemove) { availableEdges.remove(e2); } boolean stop = false; //only continue to cluster if maxSize is not reached while (clusterSize < maxSize && !stop) { //find edges that emanate and are still available emanatingEdges.addAll(g.getEdges(e.source)); emanatingEdges.addAll(g.getEdges(e.target)); emanatingEdges.retainAll(availableEdges); Node n; //get largest available edge that emanates from cluster if (!emanatingEdges.isEmpty()) { // System.out.println("Emanating edges: "+emanatingEdges); stop = true; // Set<Edge> emanatingToAdd = new HashSet<>(); // Set<Edge> emanatingToRemove = new HashSet<>(); for (Edge em : emanatingEdges) { //get node that is not yet in cluster if (cluster.nodes.contains(em.source)) { n = em.target; } else { n = em.source; } //add node to cluster and update size if n fits if (clusterSize + n.getWeight() <= maxSize) { //System.out.println("Adding " + n); availableEdges.remove(em); cluster.edges.add(em); //take all edges between two nodes in the cluster and add them to the //edges of the cluster toRemove = new HashSet<>(); for (Edge e2 : availableEdges) { if (cluster.nodes.contains(e2.source) && cluster.nodes.contains(e2.target)) { cluster.edges.add(e2); toRemove.add(e2); } } for (Edge e2 : toRemove) { availableEdges.remove(e2); } if (!cluster.nodes.contains(n)) { clusterSize = clusterSize + n.getWeight(); cluster.nodes.add(n); //itemClusterMap.get(n.toString()).add(clusters.size()); } // System.out.println("Weight is now " + clusterSize); stop = false; break; } //update emanating edges // if (n != null) { // emanatingEdges.addAll(g.getEdges(n)); // emanatingEdges.retainAll(availableEdges); // } } } else { stop = true; break; } } // System.out.println("Available edges:" + availableEdges); //result.add(cluster); } return result; }
From source file:com.shigengyu.hyperion.server.RestServer.java
private static Collection<ControllerMethod> extractControllerMethods( Iterable<ResourceProvider> resourceProviders) { TreeMap<String, ControllerMethod> controllerMethods = Maps.newTreeMap(new Comparator<String>() { @Override/*from www . ja va 2 s.c o m*/ public int compare(String first, String second) { return first.compareTo(second); } }); for (ResourceProvider resourceProvider : resourceProviders) { String controllerPath = resourceProvider.getResourceClass().getAnnotation(Path.class).value(); for (Method method : resourceProvider.getResourceClass().getMethods()) { if (!method.isAnnotationPresent(Path.class)) { continue; } String methodPath = method.getAnnotation(Path.class).value(); String httpMethod = null; if (method.isAnnotationPresent(GET.class)) { httpMethod = HttpMethods.GET; } else if (method.isAnnotationPresent(POST.class)) { httpMethod = HttpMethods.POST; } ControllerMethod controllerMethod = new ControllerMethod(httpMethod, controllerPath, methodPath); controllerMethods.put(controllerMethod.getUrl(), controllerMethod); } } return controllerMethods.values(); }
From source file:CompTest.java
public static Comparator dateComparator() { return new Comparator() { public int compare(Object o1, Object o2) { long val1 = ((Date) o1).getTime(); long val2 = ((Date) o2).getTime(); return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1)); }//from w ww .ja v a2 s . c o m }; }
From source file:org.n52.io.response.ParameterOutput.java
/** * Takes the labels to compare./* w w w.ja va 2 s . co m*/ * * @param <T> the actual type. * @return a label comparing {@link Comparator} */ public static <T extends ParameterOutput> Comparator<T> defaultComparator() { return new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.getLabel().compareTo(o2.getLabel()); } }; }
From source file:pl.bristleback.server.bristle.conf.resolver.action.IncreasingOrderSorter.java
public void sort(List<T> list) { Comparator<T> comparator = new Comparator<T>() { @Override/*from www . j a v a 2 s.co m*/ public int compare(T object1, T object2) { Order annotation1 = getSortedClass(object1).getAnnotation(Order.class); Order annotation2 = getSortedClass(object2).getAnnotation(Order.class); if (annotation2 == null) { return -1; } if (annotation1 == null) { return 1; } if (annotation1.value() > annotation2.value()) { return 1; } return -1; } }; Collections.sort(list, comparator); }
From source file:net.sourceforge.eclipsetrader.trading.internal.watchlist.ColumnRegistry.java
public static IConfigurationElement[] getProviders() { List list = new ArrayList(map.values()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { String s1 = ""; String s2 = ""; if (((IConfigurationElement) o1).getAttribute("name") != null) s1 = ((IConfigurationElement) o1).getAttribute("name"); if (((IConfigurationElement) o2).getAttribute("name") != null) s2 = ((IConfigurationElement) o2).getAttribute("name"); return s1.compareTo(s2); }/*from w w w . j ava2 s . c om*/ }); return (IConfigurationElement[]) list.toArray(new IConfigurationElement[list.size()]); }
From source file:Main.java
/** * Finds the most optimal size. The optimal size is when possible the same as * the camera resolution, if not is is it the best size between the camera solution and * MIN_SIZE_PIXELS/* w w w . jav a 2 s . c o m*/ * @param screenResolution * @param rawSupportedSizes *@param defaultCameraSize @return optimal preview size */ private static Point findBestSizeValue(Point screenResolution, List<Camera.Size> rawSupportedSizes, Camera.Size defaultCameraSize) { if (rawSupportedSizes == null) { Log.w(TAG, "Device returned no supported sizes; using default"); if (defaultCameraSize == null) { throw new IllegalStateException("Parameters contained no size!"); } return new Point(defaultCameraSize.width, defaultCameraSize.height); } // Sort by size, descending List<Camera.Size> supportedSizes = new ArrayList<>(rawSupportedSizes); Collections.sort(supportedSizes, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size a, Camera.Size b) { int aPixels = a.height * a.width; int bPixels = b.height * b.width; if (bPixels > aPixels) { return -1; } if (bPixels < aPixels) { return 1; } return 0; } }); if (Log.isLoggable(TAG, Log.INFO)) { StringBuilder sizesString = new StringBuilder(); for (Camera.Size supportedSize : supportedSizes) { sizesString.append(supportedSize.width).append('x').append(supportedSize.height).append(' '); } Log.i(TAG, "Supported sizes: " + sizesString); } double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y; // Remove sizes that are unsuitable Iterator<Camera.Size> it = supportedSizes.iterator(); while (it.hasNext()) { Camera.Size supportedSize = it.next(); int realWidth = supportedSize.width; int realHeight = supportedSize.height; if (realWidth * realHeight < MIN_SIZE_PIXELS) { it.remove(); continue; } boolean isCandidatePortrait = realWidth < realHeight; int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth; int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight; double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight; double distortion = Math.abs(aspectRatio - screenAspectRatio); if (distortion > MAX_ASPECT_DISTORTION) { it.remove(); continue; } if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) { Point exactPoint = new Point(realWidth, realHeight); Log.i(TAG, "Found size exactly matching screen size: " + exactPoint); return exactPoint; } } // If no exact match, use largest size. This was not a great idea on older devices because // of the additional computation needed. We're likely to get here on newer Android 4+ devices, where // the CPU is much more powerful. if (!supportedSizes.isEmpty()) { Camera.Size largestCameraSizes = supportedSizes.get(0); Point largestSize = new Point(largestCameraSizes.width, largestCameraSizes.height); Log.i(TAG, "Using largest suitable size: " + largestSize); return largestSize; } // If there is nothing at all suitable, return current size if (defaultCameraSize == null) { throw new IllegalStateException("Parameters contained no size!"); } Point defaultSize = new Point(defaultCameraSize.width, defaultCameraSize.height); Log.i(TAG, "No suitable sizes, using default: " + defaultSize); return defaultSize; }
From source file:com.cronutils.model.field.definition.FieldDefinition.java
/** * Creates a field definition comparator. Will compare by CronFieldName order value; * @return Comparator for FieldDefinition instance, never null; */// ww w . ja va2 s . c o m public static Comparator<FieldDefinition> createFieldDefinitionComparator() { return new Comparator<FieldDefinition>() { @Override public int compare(FieldDefinition o1, FieldDefinition o2) { return o1.getFieldName().getOrder() - o2.getFieldName().getOrder(); } }; }
From source file:utils.HttpComm.java
public static List<ProbePair> multiProbe(List<String> workersList) throws Exception { List<ProbePair> results = new LinkedList<>(); for (String workerURL : workersList) results.add(new ProbePair(workerURL, Integer.parseInt(probe(workerURL)))); Collections.sort(results, new Comparator<ProbePair>() { @Override//from w ww.j a va 2 s .com public int compare(ProbePair pair1, ProbePair pair2) { return pair1.getProbeResult() - pair2.getProbeResult(); // Ascending } }); return results; }
From source file:at.meikel.dmrl.webapp.rest.PlayerService.java
@RequestMapping(value = { "/players" }, method = RequestMethod.GET) @ResponseBody/*from ww w . jav a 2 s.c om*/ public List<Player> getAllPlayers() { List<Player> result = null; if (server != null) { result = server.getRankingList().getAllPlayers(); } if (result == null) { result = new Vector<Player>(); } Collections.sort(result, new Comparator<Player>() { @Override public int compare(Player p1, Player p2) { if (p1 == null) { return p2 == null ? 0 : 1; } else { if (p2 == null) { return -1; } else { return (int) Math.signum(p1.getRanglistenwert() - p2.getRanglistenwert()); } } } }); return result; }