All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polaris.core.service.Service Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.core.service;

import lombok.AccessLevel;
import lombok.Getter;

import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
 * @author Qt
 * @since 1.8
 */
public class Service {
	@SuppressWarnings("rawtypes")
	static final Comparator defaultComparator = (a, b) -> {
		if (Objects.equals(a, b)) {
			return 0;
		}
		int rs = Integer.compare(a.order, b.order);
		if (rs == 0) {
			rs = a.serviceClass.getName().compareTo(b.serviceClass.getName());
		}
		return rs;
	};

	@Getter
	private final Class serviceClass;
	@Getter
	private final String serviceName;
	@Getter(AccessLevel.PACKAGE)
	private final int order;
	private final Map properties;
	private volatile S singletonPure;
	private volatile S singleton;
	private Function wrapperBuilder;

	Service(final Class serviceClass, Map properties, final String serviceName, final int order, Function wrapperBuilder) {
		this.serviceClass = serviceClass;
		this.serviceName = serviceName;
		this.order = order;
		this.properties = properties;
		this.wrapperBuilder = wrapperBuilder;
	}

	public String getProperty(String name) {
		return properties == null ? null : properties.get(name);
	}

	private void build() {
		singletonPure = newPureInstance();
		singleton = wrapperBuilder == null ? singletonPure : wrapperBuilder.apply(singletonPure);
	}

	public S getPureSingleton() {
		if (singletonPure == null) {
			synchronized (this) {
				if (singletonPure == null) {
					build();
				}
			}
		}
		return singletonPure;
	}

	public S getSingleton() {
		if (singleton == null) {
			synchronized (this) {
				if (singleton == null) {
					build();
				}
			}
		}
		return singleton;
	}

	public S newInstance() {
		return wrapperBuilder == null ? newPureInstance() : wrapperBuilder.apply(newPureInstance());
	}

	public S newInstance(Class[] parameterTypes, Object[] args) {
		return wrapperBuilder == null ? newPureInstance(parameterTypes, args) : wrapperBuilder.apply(newPureInstance(parameterTypes, args));
	}

	public S newPureInstance() {
		try {
			return serviceClass.newInstance();
		} catch (ReflectiveOperationException e) {
			throw new IllegalStateException("创建实例失败", e);
		}
	}

	public S newPureInstance(Class[] parameterTypes, Object[] args) {
		try {
			return serviceClass.getConstructor(parameterTypes).newInstance(args);
		} catch (ReflectiveOperationException e) {
			throw new IllegalStateException("创建实例失败", e);
		}
	}

	@Override
	public String toString() {
		return "Service{" +
			"serviceClass=" + serviceClass.getName() +
			", serviceName=" + serviceName +
			", properties=" + properties +
			'}';
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy