/** * Like {@link #loadInstanceOf(String, Class, Class[], Object[])} for no-arg constructors. * * @param implClassName implementation class name * @param superClass superclass or interface that the implementation extends * @param <T> type of returned instance * @return instance of {@code implClassName} */ public static <T> T loadInstanceOf(String implClassName, Class<T> superClass) { return loadInstanceOf(implClassName, superClass, NO_TYPES, NO_ARGS); }
/** * Unlocks the underlying {@link Lock}. */ @Override public void close() { unlock(); }
@Test public void testDefault() throws Exception { AutoLock al = new AutoLock(); assertNotNull(al.autoLock()); assertNotNull(al.toString()); assertNotNull(al.newCondition()); al.lockInterruptibly(); al.unlock(); assertTrue(al.tryLock()); al.unlock(); assertTrue(al.tryLock(1, TimeUnit.SECONDS)); al.unlock(); }
@Test public void testLoggingCallable() throws Exception { Integer result = LoggingCallable.log(() -> 3).call(); assertEquals(3, result.intValue()); }
@Test public void testWriteLock() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); AutoReadWriteLock al = new AutoReadWriteLock(lock); assertFalse(lock.isWriteLocked()); try (AutoLock al2 = al.autoWriteLock()) { assertTrue(lock.isWriteLocked()); } assertFalse(lock.isWriteLocked()); }
@Test public void testReadLock() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); AutoReadWriteLock al = new AutoReadWriteLock(lock); assertEquals(0, lock.getReadLockCount()); try (AutoLock al2 = al.autoReadLock()) { assertEquals(1, lock.getReadLockCount()); } assertEquals(0, lock.getReadLockCount()); }
@Test public void testClose() { ReentrantLock lock = new ReentrantLock(); assertFalse(lock.isHeldByCurrentThread()); AutoLock al = new AutoLock(lock); assertFalse(lock.isHeldByCurrentThread()); al.autoLock(); assertTrue(lock.isHeldByCurrentThread()); al.close(); assertFalse(lock.isHeldByCurrentThread()); }
@Test public void testDefault() throws Exception { AutoReadWriteLock al = new AutoReadWriteLock(); assertNotNull(al.toString()); assertNotNull(al.readLock()); assertNotNull(al.writeLock()); }
@Test public void testLoadInstanceOf() { assertInstanceOf(ClassUtils.loadInstanceOf(HashSet.class), HashSet.class); }
@Test public void testRunHook() { OryxShutdownHook hook = new OryxShutdownHook(); AtomicInteger a = new AtomicInteger(); hook.addCloseable(() -> a.set(3)); hook.run(); assertEquals(3, a.get()); }
/** * @param numTasks how many copies of {@code task} to run; all may be run simultaneously, * and a shared thread pool is used * @param task task to execute; takes an {@code Integer} argument that is the index of * the task that has been executed in [0, numTasks) */ public static void doInParallel(int numTasks, Consumer<Integer> task) { doInParallel(numTasks, numTasks, false, task); }
@Test public void testExists() { assertTrue(ClassUtils.classExists("java.lang.String")); assertTrue(ClassUtils.classExists("com.cloudera.oryx.common.lang.ClassUtils")); assertFalse(ClassUtils.classExists("java.Foo")); }
/** * @return the {@link ReadWriteLock#writeLock()} ()}, locked */ public AutoLock autoWriteLock() { return writeLock.autoLock(); }
@Test public void testUsedMemory() { // Reasonable guess assertGreaterOrEqual(JVMUtils.getUsedMemory(), 1L << 20); }
/** * @return this, after calling {@link #lock()} */ public AutoLock autoLock() { lock(); return this; }
/** * @param implClassName class name to text * @return {@code true} if the class exists in the JVM and can be loaded */ public static boolean classExists(String implClassName) { try { forName(implClassName); return true; } catch (ClassNotFoundException ignored) { return false; } }
public static <V> LoggingCallable<V> log(Callable<V> delegate) { return new LoggingCallable<>(delegate); }
@Test public void testAutoClose() { ReentrantLock lock = new ReentrantLock(); assertFalse(lock.isHeldByCurrentThread()); AutoLock al = new AutoLock(lock); try (AutoLock al2 = al.autoLock()) { assertTrue(lock.isHeldByCurrentThread()); } assertFalse(lock.isHeldByCurrentThread()); }
/** * @return the {@link ReadWriteLock#readLock()}, locked */ public AutoLock autoReadLock() { return readLock.autoLock(); }