Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mytang0 committed Feb 28, 2023
1 parent 32ada24 commit 8d579ab
Show file tree
Hide file tree
Showing 31 changed files with 77 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class URL implements Serializable {

private static final long serialVersionUID = -1985165475234910535L;

private static Map<String, URL> cachedURLs = new LRUCache<>();
private static final Map<String, URL> cachedURLs = new LRUCache<>();

private final URLAddress urlAddress;
private final URLParam urlParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public final class URLBuilder extends ServiceConfigURL {

private String path;

private Map<String, String> parameters;
private final Map<String, String> parameters;

private Map<String, Object> attributes;
private final Map<String, Object> attributes;

private Map<String, Map<String, String>> methodParameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public class ScopeBeanFactory {
protected static final ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(ScopeBeanFactory.class);

private final ScopeBeanFactory parent;
private ExtensionAccessor extensionAccessor;
private List<ExtensionPostProcessor> extensionPostProcessors;
private ConcurrentHashMap<Class, AtomicInteger> beanNameIdCounterMap = new ConcurrentHashMap<>();
private List<BeanInfo> registeredBeanInfos = new CopyOnWriteArrayList<>();
private final ExtensionAccessor extensionAccessor;
private final List<ExtensionPostProcessor> extensionPostProcessors;
private final ConcurrentHashMap<Class<?>, AtomicInteger> beanNameIdCounterMap = new ConcurrentHashMap<>();
private final List<BeanInfo> registeredBeanInfos = new CopyOnWriteArrayList<>();
private InstantiationStrategy instantiationStrategy;
private AtomicBoolean destroyed = new AtomicBoolean();
private final AtomicBoolean destroyed = new AtomicBoolean();

public ScopeBeanFactory(ScopeBeanFactory parent, ExtensionAccessor extensionAccessor) {
this.parent = parent;
Expand Down Expand Up @@ -207,6 +207,7 @@ public <T> T getBean(String name, Class<T> type) {
return bean;
}

@SuppressWarnings("unchecked")
private <T> T getBeanInternal(String name, Class<T> type) {
checkDestroyed();
// All classes are derived from java.lang.Object, cannot filter bean by it
Expand Down Expand Up @@ -272,8 +273,8 @@ private void checkDestroyed() {
}

static class BeanInfo {
private String name;
private Object instance;
private final String name;
private final Object instance;

public BeanInfo(String name, Object instance) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entr
private String className;
private int type;

private Map<Object, Object> properties = new LinkedHashMap<>();
private final Map<Object, Object> properties = new LinkedHashMap<>();

public JavaBeanDescriptor() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public abstract class Mixin {
private static final String PACKAGE_NAME = Mixin.class.getPackage().getName();
private static AtomicLong MIXIN_CLASS_COUNTER = new AtomicLong(0);
private static final AtomicLong MIXIN_CLASS_COUNTER = new AtomicLong(0);

protected Mixin() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
public class FileCacheStore {
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(FileCacheStore.class);

private String cacheFilePath;
private File cacheFile;
private File lockFile;
private FileLock directoryLock;
private final String cacheFilePath;
private final File cacheFile;
private final File lockFile;
private final FileLock directoryLock;

private FileCacheStore(String cacheFilePath, File cacheFile, File lockFile, FileLock directoryLock) {
this.cacheFilePath = cacheFilePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public class Environment extends LifecycleAdapter implements ApplicationExt {

private String localMigrationRule;

private AtomicBoolean initialized = new AtomicBoolean(false);
private ScopeModel scopeModel;
private final AtomicBoolean initialized = new AtomicBoolean(false);
private final ScopeModel scopeModel;

public Environment(ScopeModel scopeModel) {
this.scopeModel = scopeModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public class ModuleEnvironment extends Environment implements ModuleExt {

public static final String NAME = "moduleEnvironment";

private AtomicBoolean initialized = new AtomicBoolean(false);
private final AtomicBoolean initialized = new AtomicBoolean(false);

private final ModuleModel moduleModel;

private Environment applicationDelegate;
private final Environment applicationDelegate;

private OrderedPropertiesConfiguration orderedPropertiesConfiguration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class OrderedPropertiesConfiguration implements Configuration {
private Properties properties;
private ModuleModel moduleModel;
private final ModuleModel moduleModel;

public OrderedPropertiesConfiguration(ModuleModel moduleModel) {
this.moduleModel = moduleModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

public class PrefixedConfiguration implements Configuration {

private String prefix;
private final String prefix;

private Configuration origin;
private final Configuration origin;

public PrefixedConfiguration(Configuration origin, String prefix) {
this.origin = origin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CompositeDynamicConfiguration implements DynamicConfiguration {

private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(CompositeDynamicConfiguration.class);

private Set<DynamicConfiguration> configurations = new HashSet<>();
private final Set<DynamicConfiguration> configurations = new HashSet<>();

public void addConfiguration(DynamicConfiguration configuration) {
if (configuration != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class AdaptiveClassCodeGenerator {

private final Class<?> type;

private String defaultExtName;
private final String defaultExtName;

public AdaptiveClassCodeGenerator(Class<?> type, String defaultExtName) {
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,27 @@ public class ExtensionLoader<T> {

private Set<Class<?>> cachedWrapperClasses;

private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<>();
private final Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<>();

private static volatile LoadingStrategy[] strategies = loadLoadingStrategies();

private static Map<String, String> specialSPILoadingStrategyMap = getSpecialSPILoadingStrategyMap();
private static final Map<String, String> specialSPILoadingStrategyMap = getSpecialSPILoadingStrategyMap();

private static SoftReference<Map<java.net.URL, List<String>>> urlListMapCache = new SoftReference<>(
new ConcurrentHashMap<>());

private static List<String> ignoredInjectMethodsDesc = getIgnoredInjectMethodsDesc();
private static final List<String> ignoredInjectMethodsDesc = getIgnoredInjectMethodsDesc();

/**
* Record all unacceptable exceptions when using SPI
*/
private Set<String> unacceptableExceptions = new ConcurrentHashSet<>();
private ExtensionDirector extensionDirector;
private List<ExtensionPostProcessor> extensionPostProcessors;
private final Set<String> unacceptableExceptions = new ConcurrentHashSet<>();
private final ExtensionDirector extensionDirector;
private final List<ExtensionPostProcessor> extensionPostProcessors;
private InstantiationStrategy instantiationStrategy;
private ActivateComparator activateComparator;
private ScopeModel scopeModel;
private AtomicBoolean destroyed = new AtomicBoolean();
private final ActivateComparator activateComparator;
private final ScopeModel scopeModel;
private final AtomicBoolean destroyed = new AtomicBoolean();

public static void setLoadingStrategies(LoadingStrategy... strategies) {
if (ArrayUtils.isNotEmpty(strategies)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static <T> Predicate<T> alwaysFalse() {
* @return non-null
*/
static <T> Predicate<T> and(Predicate<T>... predicates) {
return of(predicates).reduce((a, b) -> a.and(b)).orElseGet(Predicates::alwaysTrue);
return of(predicates).reduce(Predicate::and).orElseGet(Predicates::alwaysTrue);
}

/**
Expand All @@ -68,7 +68,7 @@ static <T> Predicate<T> and(Predicate<T>... predicates) {
* @return non-null
*/
static <T> Predicate<T> or(Predicate<T>... predicates) {
return of(predicates).reduce((a, b) -> a.or(b)).orElse(e -> true);
return of(predicates).reduce(Predicate::or).orElse(e -> true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static <T, S extends Iterable<T>> Set<T> filterSet(S values, Predicate<T> predic
return filterStream(values, predicate).collect(LinkedHashSet::new, Set::add, Set::addAll);
}

@SuppressWarnings("unchecked")
static <T, S extends Iterable<T>> S filter(S values, Predicate<T> predicate) {
final boolean isSet = Set.class.isAssignableFrom(values.getClass());
return (S) (isSet ? filterSet(values, predicate) : filterList(values, predicate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ default void execute(T t) throws RuntimeException {
* @param t the function argument
* @param consumer {@link ThrowableConsumer}
* @param <T> the source type
* @return the result after execution
*/
static <T> void execute(T t, ThrowableConsumer<T> consumer) {
consumer.execute(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class Bytes {

private static final int MASK4 = 0x0f, MASK6 = 0x3f, MASK8 = 0xff;

private static final Map<Integer, byte[]> DECODE_TABLE_MAP = new ConcurrentHashMap<Integer, byte[]>();
private static final Map<Integer, byte[]> DECODE_TABLE_MAP = new ConcurrentHashMap<>();

private static ThreadLocal<MessageDigest> MD = new ThreadLocal<MessageDigest>();
private static final ThreadLocal<MessageDigest> MD = new ThreadLocal<>();

private Bytes() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ShutdownHookCallbacks implements Disposable {

private final List<ShutdownHookCallback> callbacks = new LinkedList<>();

private ApplicationModel applicationModel;
private final ApplicationModel applicationModel;

public ShutdownHookCallbacks(ApplicationModel applicationModel) {
this.applicationModel = applicationModel;
Expand All @@ -45,7 +45,9 @@ public ShutdownHookCallbacks(ApplicationModel applicationModel) {

public ShutdownHookCallbacks addCallback(ShutdownHookCallback callback) {
synchronized (this) {
this.callbacks.add(callback);
if (!callbacks.contains(callback)) {
this.callbacks.add(callback);
}
}
return this;
}
Expand All @@ -65,7 +67,7 @@ public void destroy() {

private void loadCallbacks() {
ExtensionLoader<ShutdownHookCallback> loader =
applicationModel.getExtensionLoader(ShutdownHookCallback.class);
applicationModel.getExtensionLoader(ShutdownHookCallback.class);
loader.getSupportedExtensionInstances().forEach(this::addCallback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
public class SimpleDataStore implements DataStore {

// <component name or id, <data-name, data-value>>
private ConcurrentMap<String, ConcurrentMap<String, Object>> data =
new ConcurrentHashMap<String, ConcurrentMap<String, Object>>();
private final ConcurrentMap<String, ConcurrentMap<String, Object>> data =
new ConcurrentHashMap<>();

@Override
public Map<String, Object> get(String componentName) {
ConcurrentMap<String, Object> value = data.get(componentName);
if (value == null) {
return new HashMap<String, Object>();
return new HashMap<>();
}

return new HashMap<String, Object>(value);
return new HashMap<>(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE
}

private static class RunnableWrapper implements Runnable {
private Runnable runnable;
private final Runnable runnable;

public RunnableWrapper(Runnable runnable) {
this.runnable = runnable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Ring<T> {

AtomicInteger count = new AtomicInteger();

private List<T> itemList = new CopyOnWriteArrayList<T>();
private final List<T> itemList = new CopyOnWriteArrayList<>();

public void addItem(T t) {
if (t != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public interface AnnotationUtils {
* @param <A> the type of annotation
* @return If resolved, return the type of annotation, or <code>null</code>
*/
@SuppressWarnings("unchecked")
static <A extends Annotation> Class<A> resolveAnnotationType(AnnotatedElement annotatedElement,
String annotationClassName) {
ClassLoader classLoader = annotatedElement.getClass().getClassLoader();
Expand Down Expand Up @@ -192,6 +193,7 @@ static List<Annotation> getAllDeclaredAnnotations(AnnotatedElement annotatedElem
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
@SuppressWarnings("unchecked")
static List<Annotation> getAllDeclaredAnnotations(Class<?> type, Predicate<Annotation>... annotationsToFilter) {

if (type == null) {
Expand Down Expand Up @@ -223,6 +225,7 @@ static List<Annotation> getAllDeclaredAnnotations(Class<?> type, Predicate<Annot
* @param metaAnnotationsToFilter the meta annotations to filter
* @return non-null read-only {@link List}
*/
@SuppressWarnings("unchecked")
static List<Annotation> getMetaAnnotations(Class<? extends Annotation> annotationType,
Predicate<Annotation>... metaAnnotationsToFilter) {
return getDeclaredAnnotations(annotationType,
Expand All @@ -243,6 +246,7 @@ static List<Annotation> getMetaAnnotations(Class<? extends Annotation> annotatio
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
@SuppressWarnings("unchecked")
static List<Annotation> getAllMetaAnnotations(Class<? extends Annotation> annotationType,
Predicate<Annotation>... annotationsToFilter) {

Expand Down Expand Up @@ -280,6 +284,7 @@ static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement
* @param <A> the required type of annotation
* @return If found, return first matched-type {@link Annotation annotation}, or <code>null</code>
*/
@SuppressWarnings("unchecked")
static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
return (A) filterFirst(getAllDeclaredAnnotations(annotatedElement), a -> isSameType(a, annotationType));
}
Expand All @@ -292,6 +297,7 @@ static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement
* @param <A> the type of required annotation
* @return if found, return all matched results, or get an {@link Collections#emptyList() empty list}
*/
@SuppressWarnings("unchecked")
static <A extends Annotation> List<A> findMetaAnnotations(Class<? extends Annotation> annotationType,
Class<A> metaAnnotationType) {
return (List<A>) getAllMetaAnnotations(annotationType, a -> isSameType(a, metaAnnotationType));
Expand All @@ -305,6 +311,7 @@ static <A extends Annotation> List<A> findMetaAnnotations(Class<? extends Annota
* @param <A> the type of required annotation
* @return if found, return all matched results, or get an {@link Collections#emptyList() empty list}
*/
@SuppressWarnings("unchecked")
static <A extends Annotation> List<A> findMetaAnnotations(AnnotatedElement annotatedElement,
Class<A> metaAnnotationType) {
List<A> metaAnnotations = new LinkedList<>();
Expand Down Expand Up @@ -395,6 +402,7 @@ static boolean isAnnotationPresent(Class<?> type,
* @param annotationType the class of annotation
* @return If the specified annotation type is present, return <code>true</code>, or <code>false</code>
*/
@SuppressWarnings("unchecked")
static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotationType) {
return isAnnotationPresent(type, true, annotationType);
}
Expand All @@ -406,6 +414,7 @@ static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> an
* @param annotationClassName the class name of annotation
* @return If any specified annotation types are present, return <code>true</code>
*/
@SuppressWarnings("unchecked")
static boolean isAnnotationPresent(AnnotatedElement annotatedElement, String annotationClassName) {
ClassLoader classLoader = annotatedElement.getClass().getClassLoader();
Class<?> resolvedType = resolveClass(annotationClassName, classLoader);
Expand Down Expand Up @@ -477,6 +486,7 @@ static <T> T getDefaultValue(Annotation annotation, String attributeName) {
* @return <code>null</code> if not found
* @since 2.7.9
*/
@SuppressWarnings("unchecked")
static <T> T getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) {
Method method = findMethod(annotationType, attributeName);
return (T) (method == null ? null : method.getDefaultValue());
Expand Down
Loading

0 comments on commit 8d579ab

Please sign in to comment.