use of java.lang.ref.WeakReference in project PneumaticCraft by MineMaarten.
the class VisNetHandler method addNode.
public static WeakReference<TileVisNode> addNode(World world, TileVisNode vn) {
WeakReference ref = new WeakReference(vn);
HashMap<WorldCoordinates, WeakReference<TileVisNode>> sourcelist = sources.get(world.provider.dimensionId);
if (sourcelist == null) {
sourcelist = new HashMap<WorldCoordinates, WeakReference<TileVisNode>>();
return null;
}
ArrayList<Object[]> nearby = new ArrayList<Object[]>();
for (WeakReference<TileVisNode> root : sourcelist.values()) {
if (!isNodeValid(root))
continue;
TileVisNode source = root.get();
float r = inRange(world, vn.getLocation(), source.getLocation(), vn.getRange());
if (r > 0) {
nearby.add(new Object[] { source, r - vn.getRange() * 2 });
}
nearby = findClosestNodes(vn, source, nearby);
cache.clear();
}
float dist = Float.MAX_VALUE;
TileVisNode closest = null;
if (nearby.size() > 0) {
for (Object[] o : nearby) {
if ((Float) o[1] < dist && (vn.getAttunement() == -1 || ((TileVisNode) o[0]).getAttunement() == -1 || //) {
vn.getAttunement() == ((TileVisNode) o[0]).getAttunement()) && canNodeBeSeen(vn, (TileVisNode) o[0])) {
dist = (Float) o[1];
closest = (TileVisNode) o[0];
}
}
}
if (closest != null) {
closest.getChildren().add(ref);
nearbyNodes.clear();
return new WeakReference(closest);
}
return null;
}
use of java.lang.ref.WeakReference in project Carbon by ZieIony.
the class CarbonResources method loadDrawable.
public Drawable loadDrawable(TypedValue value, Resources.Theme theme) throws Resources.NotFoundException {
if (value == null || value.resourceId == 0) {
return null;
}
final boolean isColorDrawable;
final LongSparseArray<WeakReference<Drawable.ConstantState>> cache;
final long key;
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
isColorDrawable = true;
cache = sColorDrawableCache;
key = value.data;
} else {
isColorDrawable = false;
cache = sDrawableCache;
key = (long) value.assetCookie << 32 | value.data;
}
Drawable dr = getCachedDrawable(cache, key);
if (dr != null) {
return dr;
}
Drawable.ConstantState cs = null;
if (cs != null) {
final Drawable cloneDr = cs.newDrawable(this);
if (theme != null) {
dr = cloneDr.mutate();
applyTheme(dr, theme);
} else {
dr = cloneDr;
}
} else if (isColorDrawable) {
dr = new ColorDrawable(value.data);
} else {
dr = loadDrawableForCookie(value, value.resourceId, theme);
}
if (dr != null) {
dr.setChangingConfigurations(value.changingConfigurations);
cacheDrawable(value, theme, isColorDrawable, key, dr, cache);
}
return dr;
}
use of java.lang.ref.WeakReference in project remusic by aa112901.
the class RoundFragment method onStart.
@Override
public void onStart() {
super.onStart();
// animatorWeakReference = new WeakReference<ObjectAnimator>(new ObjectAnimator());
// animator = animatorWeakReference.get();
animatorWeakReference = new WeakReference(ObjectAnimator.ofFloat(getView(), "rotation", new float[] { 0.0F, 360.0F }));
animator = animatorWeakReference.get();
//animator = ObjectAnimator.ofFloat(getView(), "rotation", new float[]{0.0F, 360.0F});
animator.setRepeatCount(Integer.MAX_VALUE);
animator.setDuration(25000L);
animator.setInterpolator(new LinearInterpolator());
if (getView() != null)
getView().setTag(R.id.tag_animator, this.animator);
}
use of java.lang.ref.WeakReference in project dubbo by alibaba.
the class Proxy method getProxy.
/**
* Get proxy.
* @param cl class loader.
* @param ics interface class array.
*
* @return Proxy instance.
*/
public static Proxy getProxy(ClassLoader cl, Class<?>... ics) {
if (ics.length > 65535)
throw new IllegalArgumentException("interface limit exceeded");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ics.length; i++) {
String itf = ics[i].getName();
if (!ics[i].isInterface())
throw new RuntimeException(itf + " is not a interface.");
Class<?> tmp = null;
try {
tmp = Class.forName(itf, false, cl);
} catch (ClassNotFoundException e) {
}
if (tmp != ics[i])
throw new IllegalArgumentException(ics[i] + " is not visible from class loader");
sb.append(itf).append(';');
}
// use interface class name list as key.
String key = sb.toString();
// get cache by class loader.
Map<String, Object> cache;
synchronized (ProxyCacheMap) {
cache = ProxyCacheMap.get(cl);
if (cache == null) {
cache = new HashMap<String, Object>();
ProxyCacheMap.put(cl, cache);
}
}
Proxy proxy = null;
synchronized (cache) {
do {
Object value = cache.get(key);
if (value instanceof Reference<?>) {
proxy = (Proxy) ((Reference<?>) value).get();
if (proxy != null)
return proxy;
}
if (value == PendingGenerationMarker) {
try {
cache.wait();
} catch (InterruptedException e) {
}
} else {
cache.put(key, PendingGenerationMarker);
break;
}
} while (true);
}
long id = PROXY_CLASS_COUNTER.getAndIncrement();
String pkg = null;
ClassGenerator ccp = null, ccm = null;
try {
ccp = ClassGenerator.newInstance(cl);
Set<String> worked = new HashSet<String>();
List<Method> methods = new ArrayList<Method>();
for (int i = 0; i < ics.length; i++) {
if (!Modifier.isPublic(ics[i].getModifiers())) {
String npkg = ics[i].getPackage().getName();
if (pkg == null) {
pkg = npkg;
} else {
if (!pkg.equals(npkg))
throw new IllegalArgumentException("non-public interfaces from different packages");
}
}
ccp.addInterface(ics[i]);
for (Method method : ics[i].getMethods()) {
String desc = ReflectUtils.getDesc(method);
if (worked.contains(desc))
continue;
worked.add(desc);
int ix = methods.size();
Class<?> rt = method.getReturnType();
Class<?>[] pts = method.getParameterTypes();
StringBuilder code = new StringBuilder("Object[] args = new Object[").append(pts.length).append("];");
for (int j = 0; j < pts.length; j++) code.append(" args[").append(j).append("] = ($w)$").append(j + 1).append(";");
code.append(" Object ret = handler.invoke(this, methods[" + ix + "], args);");
if (!Void.TYPE.equals(rt))
code.append(" return ").append(asArgument(rt, "ret")).append(";");
methods.add(method);
ccp.addMethod(method.getName(), method.getModifiers(), rt, pts, method.getExceptionTypes(), code.toString());
}
}
if (pkg == null)
pkg = PACKAGE_NAME;
// create ProxyInstance class.
String pcn = pkg + ".proxy" + id;
ccp.setClassName(pcn);
ccp.addField("public static java.lang.reflect.Method[] methods;");
ccp.addField("private " + InvocationHandler.class.getName() + " handler;");
ccp.addConstructor(Modifier.PUBLIC, new Class<?>[] { InvocationHandler.class }, new Class<?>[0], "handler=$1;");
ccp.addDefaultConstructor();
Class<?> clazz = ccp.toClass();
clazz.getField("methods").set(null, methods.toArray(new Method[0]));
// create Proxy class.
String fcn = Proxy.class.getName() + id;
ccm = ClassGenerator.newInstance(cl);
ccm.setClassName(fcn);
ccm.addDefaultConstructor();
ccm.setSuperClass(Proxy.class);
ccm.addMethod("public Object newInstance(" + InvocationHandler.class.getName() + " h){ return new " + pcn + "($1); }");
Class<?> pc = ccm.toClass();
proxy = (Proxy) pc.newInstance();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
// release ClassGenerator
if (ccp != null)
ccp.release();
if (ccm != null)
ccm.release();
synchronized (cache) {
if (proxy == null)
cache.remove(key);
else
cache.put(key, new WeakReference<Proxy>(proxy));
cache.notifyAll();
}
}
return proxy;
}
use of java.lang.ref.WeakReference in project jna by java-native-access.
the class NativeLibraryTest method testAliasSimpleLibraryName.
public void testAliasSimpleLibraryName() throws Exception {
NativeLibrary nl = NativeLibrary.getInstance("testlib");
File file = nl.getFile();
Reference<NativeLibrary> ref = new WeakReference<NativeLibrary>(nl);
nl = null;
System.gc();
long start = System.currentTimeMillis();
while (ref.get() != null) {
Thread.sleep(10);
if ((System.currentTimeMillis() - start) > 5000L) {
fail("Timed out waiting for library to be GC'd");
}
}
TestLibrary lib = Native.loadLibrary(file.getAbsolutePath(), TestLibrary.class);
int count = lib.callCount();
TestLibrary lib2 = Native.loadLibrary("testlib", TestLibrary.class);
int count2 = lib2.callCount();
assertEquals("Simple library name not aliased", count + 1, count2);
}
Aggregations