List of usage examples for java.util Collection equals
boolean equals(Object o);
From source file:Main.java
public static boolean sameContent(final Collection<?> c1, final Collection<?> c2) { if (c1 == c2 || isEmpty(c2) && isEmpty(c1)) { return true; }//www . ja va2 s .c o m boolean same = false; if (c1 != null && c2 != null) { same = c1.size() == c2.size(); if (same) { same = c1.equals(c2); } } return same; }
From source file:Main.java
/** * INTERNAL: Compares two collections to see if they contain the same * elements.//from w w w . ja v a 2 s . c o m * * @since 1.4.1 */ public static <T> boolean equalsUnorderedSet(Collection<T> coll1, Collection<T> coll2) { // Take care of nulls if (coll1 == null) if (coll2 == null) // 1: null 2: null return true; else // 1: null 2: not null return false; else if (coll2 == null) // 1: not null 2: null return false; // Compare set size int size1 = coll1.size(); int size2 = coll2.size(); if (size1 != size2) return false; // If both have 1 element compare first element if (size1 == 1) { return Objects.equals(coll1.iterator().next(), coll2.iterator().next()); } // Compare collections as sets if (coll1 instanceof Set) if (coll2 instanceof Set) return coll1.equals(coll2); else return coll1.equals(new HashSet<T>(coll2)); else if (coll2 instanceof Set) return coll2.equals(new HashSet<T>(coll1)); else return new HashSet<T>(coll2).equals(new HashSet<T>(coll1)); }
From source file:com.redhat.jenkins.nodesharingbackend.ReservationVerifier.java
private static Map<ExecutorJenkins, PlannedFixup> computePlannedFixup(ConfigRepo.Snapshot config, Api api) { // When executor is removed from config repo, it might have ReservationTasks running for a while so it is // necessary to query these executors so the task completion can be detected. Set<ExecutorJenkins> jenkinses = new HashSet<>(config.getJenkinses()); Map<ExecutorJenkins, Map<String, ReservationTask.ReservationExecutable>> trackedReservations = trackedReservations( jenkinses);/*from w ww .ja v a 2 s.co m*/ Map<ExecutorJenkins, Set<String>> executorReservations = queryExecutorReservations(jenkinses, api); assert executorReservations.keySet().equals(trackedReservations.keySet()) : executorReservations + " != " + trackedReservations; // TODO verify multiple executors are not using same host // TODO the executor might no longer use the plugin Map<ExecutorJenkins, PlannedFixup> plan = new HashMap<>(); for (Map.Entry<ExecutorJenkins, Set<String>> er : executorReservations.entrySet()) { ExecutorJenkins executor = er.getKey(); @CheckForNull Collection<String> utilizedNodes = er.getValue(); // Might fail getting the data Collection<String> reservedNodes = trackedReservations.get(executor).keySet(); // Failed to query the host - no balancing if (utilizedNodes == null) { if (!reservedNodes.isEmpty()) { LOGGER.warning( "Failed to query executor " + executor.getName() + " with reserved nodes tracked"); } continue; } if (utilizedNodes.equals(reservedNodes)) continue; // In sync ArrayList<String> toSchedule = new ArrayList<>(utilizedNodes); toSchedule.removeAll(reservedNodes); ArrayList<String> toCancel = new ArrayList<>(reservedNodes); toCancel.removeAll(utilizedNodes); plan.put(executor, new PlannedFixup(toCancel, toSchedule)); } return plan; }
From source file:de.dhke.projects.cutil.collections.aspect.AspectMultiMapEntrySet.java
@Override public boolean remove(final Object o) { if (o instanceof Map.Entry) { final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o; if (_aspectMap.containsKey(entry.getKey())) { Collection<V> values = _aspectMap.get(entry.getKey()); if (values.equals(entry.getValue())) { _aspectMap.remove(entry.getKey()); return true; }/*w w w . j a v a2 s . c o m*/ } } return false; }
From source file:edu.uci.ics.jung.graph.SetHypergraph.java
/** * Adds <code>hyperedge</code> to this graph and connects them to the vertex collection <code>to_attach</code>. * Any vertices in <code>to_attach</code> that appear more than once will only appear once in the * incident vertex collection for <code>hyperedge</code>, that is, duplicates will be ignored. * //from w w w. j a v a 2 s . com * @see Hypergraph#addEdge(Object, Collection) */ public boolean addEdge(H hyperedge, Collection<? extends V> to_attach) { if (hyperedge == null) throw new IllegalArgumentException("input hyperedge may not be null"); if (to_attach == null) throw new IllegalArgumentException("endpoints may not be null"); if (to_attach.contains(null)) throw new IllegalArgumentException("cannot add an edge with a null endpoint"); Set<V> new_endpoints = new HashSet<V>(to_attach); if (edges.containsKey(hyperedge)) { Collection<V> attached = edges.get(hyperedge); if (!attached.equals(new_endpoints)) { throw new IllegalArgumentException( "Edge " + hyperedge + " exists in this graph with endpoints " + attached); } else return false; } edges.put(hyperedge, new_endpoints); for (V v : to_attach) { // add v if it's not already in the graph addVertex(v); // associate v with hyperedge vertices.get(v).add(hyperedge); } return true; }
From source file:net.rim.ejde.internal.ui.launchers.AbstractLaunchShortcut.java
/** * Search launch configurations in given type by projects and VM. If one could not be found, creates one; if multiple * configurations are found, prompt a dialog for user selection. * * @param selectedProjects/*from ww w . j a va 2s.c o m*/ * The selected projects * @param configType * The launch configuration type * @return The launch configuration */ private ILaunchConfiguration findLaunchConfiguration(Set<IProject> selectedProjects, ILaunchConfigurationType configType) { List<ILaunchConfiguration> candidateConfigs = new ArrayList<ILaunchConfiguration>(); try { ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager() .getLaunchConfigurations(configType); for (int i = 0; i < configs.length; i++) { // Compare projects and VM in the launch configuration and selected projects Collection<IProject> projects = LaunchUtils.getProjectsFromConfiguration(configs[i]); if (projects.equals(selectedProjects)) { candidateConfigs.add(configs[i]); } } } catch (CoreException e) { _logger.error(e.getMessage(), e); } // If there are no existing configs associated with the IType, create // one. // If there is exactly one config associated with the IType, return it. // Otherwise, if there is more than one config associated with the // IType, prompt the user to choose one. int candidateCount = candidateConfigs.size(); ILaunchConfiguration ret; if (candidateCount < 1) { ret = createConfiguration(selectedProjects); } else if (candidateCount == 1) { ret = candidateConfigs.get(0); } else { // Prompt the user to choose a config. A null result means the user // canceled the dialog, in which case this method returns null, // since canceling the dialog should also cancel launching anything. ret = chooseConfiguration(candidateConfigs); } return ret; }
From source file:com.jakemadethis.graphite.graph.OrderedHypergraph.java
/** * Adds <code>hyperedge</code> to this graph and connects them to the vertex collection <code>to_attach</code>. * The order in which vertices appear in <code>to_attach</code> is the order that they will appear * in the incident vertex collection/*from www . jav a2s.c o m*/ * * @see Hypergraph#addEdge(Object, Collection) */ public boolean addEdge(H hyperedge, Collection<? extends V> to_attach) { if (hyperedge == null) throw new IllegalArgumentException("input hyperedge may not be null"); if (to_attach == null) throw new IllegalArgumentException("endpoints may not be null"); if (to_attach.contains(null)) throw new IllegalArgumentException("cannot add an edge with a null endpoint"); ArrayList<V> new_endpoints = new ArrayList<V>(to_attach); if (edges.containsKey(hyperedge)) { Collection<V> attached = edges.get(hyperedge); if (!attached.equals(new_endpoints)) { throw new IllegalArgumentException( "Edge " + hyperedge + " exists in this graph with endpoints " + attached); } else return false; } edges.put(hyperedge, new_endpoints); for (V v : to_attach) { // add v if it's not already in the graph addVertex(v); // associate v with hyperedge vertices.get(v).add(hyperedge); } return true; }
From source file:com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.java
public void validate(Object object, String context, ValidatorContext validatorContext, String method) throws ValidationException { List<Validator> validators = getValidators(object.getClass(), context, method); Set<String> shortcircuitedFields = null; for (final Validator validator : validators) { try {//from w w w .java 2s . c o m validator.setValidatorContext(validatorContext); if (LOG.isDebugEnabled()) { LOG.debug( "Running validator: " + validator + " for object " + object + " and method " + method); } FieldValidator fValidator = null; String fullFieldName = null; if (validator instanceof FieldValidator) { fValidator = (FieldValidator) validator; fullFieldName = new InternalValidatorContextWrapper(fValidator.getValidatorContext()) .getFullFieldName(fValidator.getFieldName()); if ((shortcircuitedFields != null) && shortcircuitedFields.contains(fullFieldName)) { if (LOG.isDebugEnabled()) { LOG.debug("Short-circuited, skipping"); } continue; } } if (validator instanceof ShortCircuitableValidator && ((ShortCircuitableValidator) validator).isShortCircuit()) { // get number of existing errors List<String> errs = null; if (fValidator != null) { if (validatorContext.hasFieldErrors()) { Collection<String> fieldErrors = validatorContext.getFieldErrors().get(fullFieldName); if (fieldErrors != null) { errs = new ArrayList<String>(fieldErrors); } } } else if (validatorContext.hasActionErrors()) { Collection<String> actionErrors = validatorContext.getActionErrors(); if (actionErrors != null) { errs = new ArrayList<String>(actionErrors); } } validator.validate(object); if (fValidator != null) { if (validatorContext.hasFieldErrors()) { Collection<String> errCol = validatorContext.getFieldErrors().get(fullFieldName); if ((errCol != null) && !errCol.equals(errs)) { if (LOG.isDebugEnabled()) { LOG.debug("Short-circuiting on field validation"); } if (shortcircuitedFields == null) { shortcircuitedFields = new TreeSet<String>(); } shortcircuitedFields.add(fullFieldName); } } } else if (validatorContext.hasActionErrors()) { Collection<String> errCol = validatorContext.getActionErrors(); if ((errCol != null) && !errCol.equals(errs)) { if (LOG.isDebugEnabled()) { LOG.debug("Short-circuiting"); } break; } } continue; } validator.validate(object); } finally { validator.setValidatorContext(null); } } }
From source file:com.knowbout.cc2nlp.server.CCEventServiceImpl.java
/** * //from w w w .j a v a2 s . co m * @param program * @param keywords * @return */ private boolean areKeywordsChanged(Program program, Collection<Keyword> keywords) { Collection<Keyword> previous = lastKeywordList.get(program.getChannel()); lastKeywordList.put(program.getChannel(), keywords); if (previous == null) { return true; } else { return !previous.equals(keywords); } }
From source file:com.adobe.acs.commons.wcm.impl.PropertyMergePostProcessor.java
/** * Merges the values found in the the source properties into the destination * property as a multi-value. The values of the source properties and * destination properties must all be the same property type. * * The unique set of properties will be stored in * * @param resource the resource to look for the source and destination * properties on/*from ww w . ja va2 s . c om*/ * @param destination the property to store the collected properties. * @param sources the properties to collect values from for merging * @param typeHint the data type that should be used when reading and * storing the data * @param allowDuplicates true to allow duplicates values in the destination * property; false to make values unique * @return Optional resource updated, if any */ protected final <T> Optional<Resource> merge(final Resource resource, final String destination, final Collection<String> sources, final Class<T> typeHint, final boolean allowDuplicates) throws PersistenceException { ResourceResolver rr = resource.getResourceResolver(); // Create an empty array of type T @SuppressWarnings("unchecked") final T[] emptyArray = (T[]) Array.newInstance(typeHint, 0); Collection<T> collectedValues; if (allowDuplicates) { collectedValues = new ArrayList<>(); } else { collectedValues = new LinkedHashSet<>(); } for (final String source : sources) { Resource sourceProperties = resource; String sourceParam = source; if (source.contains("/")) { sourceParam = StringUtils.substringAfterLast(source, "/"); sourceProperties = rr.getResource(resource, StringUtils.substringBeforeLast(source, "/")); } T[] tmp = sourceProperties.adaptTo(ModifiableValueMap.class).get(sourceParam, emptyArray); collectedValues.addAll(Arrays.asList(tmp)); } Resource targetResource = resource; String targetProperty = destination; if (destination.contains("/")) { targetProperty = StringUtils.substringAfterLast(destination, "/"); targetResource = rr.getResource(resource, StringUtils.substringBeforeLast(destination, "/")); } ModifiableValueMap targetProperties = targetResource.adaptTo(ModifiableValueMap.class); final T[] currentValues = targetProperties.get(targetProperty, emptyArray); if (!collectedValues.equals(Arrays.asList(currentValues))) { targetProperties.put(targetProperty, collectedValues.toArray(emptyArray)); return Optional.of(targetResource); } else { return Optional.empty(); } }