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());
}
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;
}
};
}
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;
}
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());
}
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;
}
Aggregations