Search in sources :

Example 66 with Collection

use of java.util.Collection in project j2objc by google.

the class IdentityHashMapTest method test_values.

/**
	 * @tests java.util.IdentityHashMap#values()
	 */
public void test_values() {
    IdentityHashMap map = new IdentityHashMap();
    for (int i = 0; i < 10; i++) {
        map.put(new Integer(i), new Integer(i));
    }
    Integer key = new Integer(20);
    Integer value = new Integer(40);
    map.put(key, value);
    Collection vals = map.values();
    boolean result = vals.remove(key);
    assertTrue("removed entries incorrectly", map.size() == 11 && !result);
    assertTrue("removed key incorrectly", map.containsKey(key));
    assertTrue("removed value incorrectly", map.containsValue(value));
    result = vals.remove(value);
    assertTrue("Did not remove entry as expected", map.size() == 10 && result);
    assertTrue("Did not remove key as expected", !map.containsKey(key));
    assertTrue("Did not remove value as expected", !map.containsValue(value));
    // put an equivalent key to a value
    key = new Integer(1);
    value = new Integer(100);
    map.put(key, value);
    result = vals.remove(key);
    assertTrue("TestB. removed entries incorrectly", map.size() == 11 && !result);
    assertTrue("TestB. removed key incorrectly", map.containsKey(key));
    assertTrue("TestB. removed value incorrectly", map.containsValue(value));
    result = vals.remove(value);
    assertTrue("TestB. Did not remove entry as expected", map.size() == 10 && result);
    assertTrue("TestB. Did not remove key as expected", !map.containsKey(key));
    assertTrue("TestB. Did not remove value as expected", !map.containsValue(value));
    vals.clear();
    assertEquals("Did not remove all entries as expected", 0, map.size());
}
Also used : IdentityHashMap(java.util.IdentityHashMap) Collection(java.util.Collection)

Example 67 with Collection

use of java.util.Collection in project gradle by gradle.

the class DefaultLenientConfiguration method select.

@Override
public SelectedArtifactSet select(final Spec<? super Dependency> dependencySpec, final AttributeContainerInternal requestedAttributes, final Spec<? super ComponentIdentifier> componentSpec) {
    final SelectedArtifactResults artifactResults;
    final SelectedFileDependencyResults fileDependencyResults;
    VariantSelector selector = artifactTransforms.variantSelector(requestedAttributes);
    artifactResults = this.artifactResults.select(componentSpec, selector);
    fileDependencyResults = this.fileDependencyResults.select(componentSpec, selector);
    return new SelectedArtifactSet() {

        @Override
        public <T extends Collection<Object>> T collectBuildDependencies(T dest) {
            artifactResults.getArtifacts().collectBuildDependencies(dest);
            fileDependencyResults.getArtifacts().collectBuildDependencies(dest);
            return dest;
        }

        @Override
        public void visitArtifacts(ArtifactVisitor visitor) {
            DefaultLenientConfiguration.this.visitArtifacts(dependencySpec, artifactResults, fileDependencyResults, visitor);
        }

        /**
             * Collects files reachable from first level dependencies that satisfy the given spec. Fails when any file cannot be resolved
             */
        @Override
        public <T extends Collection<? super File>> T collectFiles(T dest) throws ResolveException {
            rethrowFailure();
            ResolvedFilesCollectingVisitor visitor = new ResolvedFilesCollectingVisitor(dest);
            try {
                DefaultLenientConfiguration.this.visitArtifacts(dependencySpec, artifactResults, fileDependencyResults, visitor);
                // The visitor adds file dependencies directly to the destination collection however defers adding the artifacts.
                // This is to ensure a fixed order regardless of whether the first level dependencies are filtered or not
                // File dependencies and artifacts are currently treated separately as a migration step
                visitor.addArtifacts();
            } catch (Throwable t) {
                visitor.failures.add(t);
            }
            if (!visitor.failures.isEmpty()) {
                throw new ArtifactResolveException("files", configuration.getPath(), configuration.getDisplayName(), visitor.failures);
            }
            return dest;
        }

        /**
             * Collects all resolved artifacts. Fails when any artifact cannot be resolved.
             */
        @Override
        public <T extends Collection<? super ResolvedArtifactResult>> T collectArtifacts(T dest) throws ResolveException {
            rethrowFailure();
            ResolvedArtifactCollectingVisitor visitor = new ResolvedArtifactCollectingVisitor(dest);
            try {
                DefaultLenientConfiguration.this.visitArtifacts(dependencySpec, artifactResults, fileDependencyResults, visitor);
            } catch (Throwable t) {
                visitor.failures.add(t);
            }
            if (!visitor.failures.isEmpty()) {
                throw new ArtifactResolveException("artifacts", configuration.getPath(), configuration.getDisplayName(), visitor.failures);
            }
            return dest;
        }
    };
}
Also used : SelectedFileDependencyResults(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.SelectedFileDependencyResults) SelectedArtifactResults(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.SelectedArtifactResults) VariantSelector(org.gradle.api.internal.artifacts.transform.VariantSelector) SelectedArtifactSet(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.SelectedArtifactSet) ArtifactVisitor(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ArtifactVisitor) Collection(java.util.Collection) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) DefaultResolvedArtifactResult(org.gradle.api.internal.artifacts.result.DefaultResolvedArtifactResult) File(java.io.File)

Example 68 with Collection

use of java.util.Collection in project tray by grandcentrix.

the class ChangedListenerTest method checkChangeListener.

private ContentProviderStorage checkChangeListener(boolean registerWithLooper, final OnTrayPreferenceChangeListener otherListener) throws Exception {
    final ContentProviderStorage userStorage = new ContentProviderStorage(getProviderMockContext(), "testChanged", TrayStorage.Type.USER);
    final CountDownLatch listenerCalledLatch = new CountDownLatch(1);
    final boolean[] listenerCalled = { false };
    final Looper[] looperRegisteredOn = { null };
    final OnTrayPreferenceChangeListener listener = new OnTrayPreferenceChangeListener() {

        @Override
        public void onTrayPreferenceChanged(final Collection<TrayItem> items) {
            // check if called on the correct looper if one is set.
            assertEquals(looperRegisteredOn[0], Looper.myLooper());
            listenerCalled[0] = true;
            listenerCalledLatch.countDown();
        }
    };
    final CountDownLatch registerLatch = new CountDownLatch(1);
    if (registerWithLooper) {
        // registers in a thread with a looper
        new HandlerThread("register") {

            @Override
            protected void onLooperPrepared() {
                super.onLooperPrepared();
                looperRegisteredOn[0] = Looper.myLooper();
                userStorage.registerOnTrayPreferenceChangeListener(listener);
                if (otherListener != null) {
                    userStorage.registerOnTrayPreferenceChangeListener(otherListener);
                }
                registerLatch.countDown();
            }
        }.start();
    } else {
        // registers in a thread without a looper
        new Thread(new Runnable() {

            @Override
            public void run() {
                userStorage.registerOnTrayPreferenceChangeListener(listener);
                registerLatch.countDown();
            }
        }).start();
    }
    registerLatch.await(1000, TimeUnit.MILLISECONDS);
    assertNotNull(userStorage.mObserver);
    assertNotNull(userStorage.mObserverThread);
    assertFalse(listenerCalled[0]);
    final TrayUri trayUri = new TrayUri(getProviderMockContext());
    final Uri uri = trayUri.get();
    if (registerWithLooper) {
        // if not a looper thread the handler in the onChange method does not fire
        new HandlerThread("change") {

            @Override
            protected void onLooperPrepared() {
                super.onLooperPrepared();
                // in a perfect world I could change a value in the storage an it will call the
                // listener. But this is andorid and testing is hard. Even harder with threading.
                // The hardest thing here is the Isolated context, and the dependency to the Looper
                // to get the change from the content provider.
                //
                // tl;dr the ContentObserver does not work in a ProviderTestCase2 so I call the
                // listener myself instead of changing data.
                //
                // wasted hours so far: 12
                // userStorage.put("the", "change");
                userStorage.mObserver.onChange(false, uri);
            }
        }.start();
    } else {
        new Thread(new Runnable() {

            @Override
            public void run() {
                // see explanation above
                // userStorage.put("the", "change");
                userStorage.mObserver.onChange(false, uri);
            }
        }).start();
    }
    listenerCalledLatch.await(3000, TimeUnit.MILLISECONDS);
    assertTrue(listenerCalled[0]);
    userStorage.unregisterOnTrayPreferenceChangeListener(listener);
    return userStorage;
}
Also used : OnTrayPreferenceChangeListener(net.grandcentrix.tray.core.OnTrayPreferenceChangeListener) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) HandlerThread(android.os.HandlerThread) Looper(android.os.Looper) HandlerThread(android.os.HandlerThread) Collection(java.util.Collection)

Example 69 with Collection

use of java.util.Collection in project tray by grandcentrix.

the class ChangedListenerTest method testApiLevel15OnChange.

public void testApiLevel15OnChange() throws Exception {
    final CountDownLatch latch = new CountDownLatch(// first time called in checkChangeListener() second time in this test
    2);
    final ArrayList<TrayItem> changed = new ArrayList<>();
    final OnTrayPreferenceChangeListener listener = new OnTrayPreferenceChangeListener() {

        @Override
        public void onTrayPreferenceChanged(final Collection<TrayItem> items) {
            changed.addAll(items);
            latch.countDown();
        }
    };
    final ContentProviderStorage storage = checkChangeListener(true, listener);
    storage.put("some", "value");
    storage.put("foo", "bar");
    new HandlerThread("change") {

        @Override
        protected void onLooperPrepared() {
            super.onLooperPrepared();
            storage.mObserver.onChange(false);
        }
    }.start();
    latch.await(3000, TimeUnit.MILLISECONDS);
    assertEquals(2, changed.size());
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) Collection(java.util.Collection) OnTrayPreferenceChangeListener(net.grandcentrix.tray.core.OnTrayPreferenceChangeListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 70 with Collection

use of java.util.Collection in project groovy-core by groovy.

the class GroovyTypeCheckingExtensionSupport method handleAmbiguousMethods.

@Override
@SuppressWarnings("unchecked")
public List<MethodNode> handleAmbiguousMethods(final List<MethodNode> nodes, final Expression origin) {
    List<Closure> onMethodSelection = eventHandlers.get("handleAmbiguousMethods");
    List<MethodNode> methodList = nodes;
    if (onMethodSelection != null) {
        Iterator<Closure> iterator = onMethodSelection.iterator();
        while (methodList.size() > 1 && iterator.hasNext()) {
            final Closure closure = iterator.next();
            Object result = safeCall(closure, methodList, origin);
            if (result != null) {
                if (result instanceof MethodNode) {
                    methodList = Collections.singletonList((MethodNode) result);
                } else if (result instanceof Collection) {
                    methodList = new LinkedList<MethodNode>((Collection<? extends MethodNode>) result);
                } else {
                    throw new GroovyBugError("Type checking extension returned unexpected method list: " + result);
                }
            }
        }
    }
    return methodList;
}
Also used : Closure(groovy.lang.Closure) MethodNode(org.codehaus.groovy.ast.MethodNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Collection(java.util.Collection) LinkedList(java.util.LinkedList)

Aggregations

Collection (java.util.Collection)3392 ArrayList (java.util.ArrayList)983 Map (java.util.Map)732 Test (org.junit.Test)669 HashMap (java.util.HashMap)570 List (java.util.List)521 Iterator (java.util.Iterator)386 HashSet (java.util.HashSet)333 Set (java.util.Set)314 IOException (java.io.IOException)308 Collectors (java.util.stream.Collectors)155 File (java.io.File)135 LinkedHashMap (java.util.LinkedHashMap)112 Collections (java.util.Collections)100 LinkedList (java.util.LinkedList)96 Arrays (java.util.Arrays)84 UUID (java.util.UUID)80 Test (org.testng.annotations.Test)79 NotNull (org.jetbrains.annotations.NotNull)78 Field (java.lang.reflect.Field)77