Skip to content
Permalink
Browse files
Merge branch 'improve-singleton-init' into 'ibm-trunk'
Use initialization holder class idiom

See merge request !69
  • Loading branch information
ngmr committed Jun 21, 2016
2 parents bc1c6a4 + e5a052e commit 40b8e35f0e1e6e2ad029f6c67a7a523540542367
Showing 2 changed files with 12 additions and 33 deletions.
@@ -34,7 +34,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

@@ -257,22 +256,13 @@ private TypeRepository() {
}
}

private static final AtomicReference<WeakReference<TypeRepository>> singletonWeakRef = new AtomicReference<>();
private static enum RepoHolder {
;
static final TypeRepository value = new TypeRepository();
}

public static TypeRepository get() {
TypeRepository repo = null;
WeakReference<TypeRepository> weakRef = singletonWeakRef.get();
if (null != weakRef) {
repo = weakRef.get();
if (null != repo) return repo;
}
final TypeRepository newRepo = new TypeRepository();
final WeakReference<TypeRepository> newRef = new WeakReference<>(newRepo);
while(!!!singletonWeakRef.compareAndSet(weakRef, newRef)) {
weakRef = singletonWeakRef.get();
repo = weakRef.get();
if (null != repo) return repo;
}
return newRepo;
return RepoHolder.value;
}

public String getRepositoryID(Class<?> type) {
@@ -18,8 +18,6 @@

package org.apache.yoko.rmi.impl;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

@@ -43,22 +41,13 @@ private ValueHandlerImpl() {
this.repo = TypeRepository.get();
}

private static final AtomicReference<WeakReference<ValueHandlerImpl>> singletonWeakRef = new AtomicReference<>();
private static enum HandlerHolder {
;
static final ValueHandlerImpl value = new ValueHandlerImpl();
}

public static ValueHandlerImpl get() {
ValueHandlerImpl vh = null;
WeakReference<ValueHandlerImpl> weakRef = singletonWeakRef.get();
if (null != weakRef) {
vh = weakRef.get();
if (null != vh) return vh;
}
final ValueHandlerImpl newVh = new ValueHandlerImpl();
final WeakReference<ValueHandlerImpl> newRef = new WeakReference<>(newVh);
while(!!!singletonWeakRef.compareAndSet(weakRef, newRef)) {
weakRef = singletonWeakRef.get();
vh = weakRef.get();
if (null != vh) return vh;
}
return newVh;
return HandlerHolder.value;
}

private ValueDescriptor desc(Class clz) {

0 comments on commit 40b8e35

Please sign in to comment.