Java源码示例:org.osgl.Lang
示例1
public List<BeanSpec> componentSpecs() {
if (!componentSpecsSet) {
synchronized (this) {
if (!componentSpecsSet) {
componentSpecsSet = true;
if (isArray()) {
BeanSpec componentSpec = BeanSpec.of(rawType.getComponentType(), injector);
componentSpecs = C.list(componentSpec);
} else {
componentSpecs = C.list(typeParams).map(new Lang.Transformer<Type, BeanSpec>() {
@Override
public BeanSpec transform(Type type) {
return BeanSpec.of(type, injector);
}
});
}
}
}
}
return componentSpecs;
}
示例2
@Command(name = "act.plugin.list,act.plugin,act.plugins", help = "list plugins")
@PropertySpec("this as Plugin")
public List<String> list(
@Optional("sort alphabetically") boolean sort,
@Optional("filter plugin") final String q
) {
C.List<String> l = C.list(Plugin.InfoRepo.plugins());
if (S.notBlank(q)) {
l = l.filter(new Lang.Predicate<String>() {
@Override
public boolean test(String s) {
return s.toLowerCase().contains(q) || s.matches(q);
}
});
}
return sort ? l.sorted() : l;
}
示例3
private App() {
this.version = Version.get();
this.appBase = new File(".");
this.layout = ProjectLayout.PredefinedLayout.MAVEN;
this.appHome = RuntimeDirs.home(this);
this.config = new AppConfig<>().app(this);
INST = this;
Lang.setFieldValue("metricPlugin", Act.class, new SimpleMetricPlugin());
this.eventEmitted = new HashSet<>();
this.eventBus = new EventBus(this);
this.jobManager = new JobManager(this);
this.classLoader = new AppClassLoader(this);
this.sessionManager = new SessionManager(this.config, config().cacheService("logout-session"));
this.dependencyInjector = new GenieInjector(this);
this.singletonRegistry = new SingletonRegistry(this);
this.singletonRegistry.register(SingletonRegistry.class, this.singletonRegistry);
this.singletonRegistry.register(SessionManager.class, this.sessionManager);
this.singletonRegistry.register(CookieSessionMapper.class, new CookieSessionMapper(this.config));
this.idGenerator = new IdGenerator();
}
示例4
@Command(name = "act.singleton.list, act.singleton, act.singletons", help = "List all singletons")
public Iterable<String> listSingletons(@Optional("specify singleton filter") final String q) {
SingletonRegistry singletonRegistry = Act.app().singletonRegistry();
final Iterable<String> iterable = singletonRegistry.typeNames();
if (S.notBlank(q)) {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return Iterators.filter(iterable.iterator(), new Lang.Predicate<String>() {
@Override
public boolean test(String s) {
return s.toLowerCase().contains(q) || s.matches(q);
}
});
}
};
}
return iterable;
}
示例5
private void handleForSerializer(final ObjectSerializer serializer, Class targetType) {
ClassNode node = repo.node(targetType.getName());
if (null == node) {
warn("Unknown target type: " + targetType.getName());
return;
}
final SerializeConfig config = SerializeConfig.getGlobalInstance();
node.visitSubTree(new Lang.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws Lang.Break {
Class type = app.classForName(classNode.name());
config.put(type, serializer);
}
});
config.put(targetType, serializer);
}
示例6
private void handleForDeserializer(final ObjectDeserializer deserializer, Class targetType) {
ClassNode node = repo.node(targetType.getName());
if (null == node) {
warn("Unknown target type: " + targetType.getName());
return;
}
final ParserConfig config = ParserConfig.getGlobalInstance();
node.visitSubTree(new Lang.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws Lang.Break {
Class type = app.classForName(classNode.name());
config.putDeserializer(type, deserializer);
}
});
config.putDeserializer(targetType, deserializer);
}
示例7
@OnAppStart(async = true)
public void loadEligibleEnums(ClassInfoRepository repo, final App app) {
final ClassNode enumRoot = repo.node(Enum.class.getName());
final $.Predicate<String> tester = app.config().appClassTester();
enumRoot.visitSubTree(new Lang.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws Lang.Break {
String name = classNode.name();
if (tester.test(name) && !name.startsWith("act.")) {
Class<? extends Enum> enumType = $.cast(app.classForName(name));
Object[] constants = enumType.getEnumConstants();
if (null != constants && constants.length > 0) {
String simpleName = enumType.getSimpleName();
Class<?> existing = eligibleEnums.get(simpleName);
if (null != existing && existing.getName().equals(enumType.getName())) {
warn("Ambiguous enum name found between %s and %s", existing.getName(), enumType.getName());
} else {
eligibleEnums.put(simpleName, enumType);
}
}
}
}
});
eligibleEnumSize = eligibleEnums.size();
}
示例8
@Override
public Lang.T2<C.List<T>, C.List<T>> split(final Lang.Function<? super T, Boolean> predicate) {
final C.List<T> left = C.newList();
final C.List<T> right = C.newList();
accept(new $.Visitor<T>() {
@Override
public void visit(T t) throws Lang.Break {
if (predicate.apply(t)) {
left.add(t);
} else {
right.add(t);
}
}
});
if (isImmutable() || isReadOnly()) {
return $.T2(C.list(left), C.list(right));
}
return $.T2(left, right);
}
示例9
/**
* A function that generates a image with random picked pixels with random color. The image
* use `background` color as the background
*
* @param w the width
* @param h the height
* @param percent the percent of pixels selected from all pixels in the image
* @param background the background color
* @return a function as described above
*/
public static $.Producer<BufferedImage> randomPixels(final int w, final int h, final int percent, final Color background) {
return background(w, h, $.val(background)).andThen(new $.Function<BufferedImage, BufferedImage>() {
@Override
public BufferedImage apply(BufferedImage img) throws NotAppliedException, Lang.Break {
java.util.Random r = ThreadLocalRandom.current();
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
if (r.nextInt(100) < percent) {
int v = Img.randomColorValue(false);
img.setRGB(i, j, v);
}
}
}
return img;
}
});
}
示例10
protected boolean probeBinary() {
String contentType = getContentType();
if (null != contentType) {
MimeType mimeType = MimeType.findByContentType(contentType);
if (null != mimeType) {
return !mimeType.test(MimeType.Trait.text);
}
}
final $.Var<Boolean> var = new $.Var<>(false);
consumeOnce(new $.F1<InputStream, Object>() {
@Override
public Object apply(InputStream is) throws NotAppliedException, Lang.Break {
boolean isBinary = OsglConfig.binaryDataProbe().apply($.convert(is).to(Reader.class));
var.set(isBinary);
return null;
}
});
return var.get();
}
示例11
@Override
public Font apply(String text) throws NotAppliedException, Lang.Break {
Random r = ThreadLocalRandom.current();
int style = randomStyle(r);
String fontName = $.random(families);
int size = MIN_SIZE + r.nextInt(MAX_SIZE - MIN_SIZE);
return new Font(fontName, style, size);
}
示例12
@GetAction("{encrypted}")
public Result renderMedia(String encrypted) {
try {
final String text = crypto.decrypt(encrypted);
return renderBinary(new Lang.Visitor<Output>() {
@Override
public void visit(Output output) throws Lang.Break {
CaptchaImageRender imageRender = pluginManager.randomImageRender();
Img.source(imageRender.render(text)).writeTo(Output.Adaptors.asOutputStream(output), JPG.contentType());
}
}).contentType(JPG);
} catch (Exception e) {
throw notFound();
}
}
示例13
@Override
public void visit(GcInfo gcInfo) throws Lang.Break {
currentCounter.incrementAndGet();
if (smallWindowSum.incrementAndGet() > 10) {
// there are at least 11 gc happened within last 2s
pauseNow();
wakeUp();
}
largeWindowSum.incrementAndGet();
LOGGER.debug("gc notified. gc summary: %s | %s", smallWindowSum.get(), largeWindowSum.get());
}
示例14
@Override
public void visit(Object o) throws Lang.Break {
if (null == o) {
return;
}
$.setFieldValue(o, field, principalProvider.get());
}
示例15
/**
* Find the permutation of arg types. E.g
*
* Suppose a declared argTypes is:
*
* ```
* (List, ISObject)
* ```
*
* Then permutation of the argTypes includes:
*
* ```
* (List, ISObject)
* (ArrayList, ISObject)
* (LinkedList, ISObject)
* (ArrayList, SObject)
* (LinkedList, SObject)
* ...
* ```
*
* @param argTypes
* @return
*/
private static Set<List<Class>> permutationOf(List<Class> argTypes) {
int len = argTypes.size();
if (len == 0) {
return C.Set();
}
// get type candidates for each arg position
final AppClassLoader classLoader = Act.app().classLoader();
ClassInfoRepository repo = classLoader.classInfoRepository();
final List<List<Class>> candidates = new ArrayList<>();
for (int i = 0; i < len; ++i) {
Class type = argTypes.get(i);
final List<Class> list = new ArrayList<>();
list.add(type);
candidates.add(list);
ClassNode node = repo.findNode(type);
if (null != node) {
node.visitPublicSubTreeNodes(new Lang.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws Lang.Break {
list.add(Act.classForName(classNode.name()));
}
});
}
}
// generate permutation of argTypes
return permutationOf(candidates, candidates.size() - 1);
}
示例16
FieldLoader(Field field, ParamValueLoader loader, ParamValueLoader stringValueLoader, Lang.TypeConverter<Object, Object> converter) {
Class<?> type = field.getType();
boolean isString = String.class == type;
boolean isCharArray = char[].class == type;
this.isString = isString;
this.isSensitive = isString && null != field.getAnnotation(Sensitive.class);
this.isPassword = (isString || isCharArray) && null != field.getAnnotation(Password.class);
this.field = field;
this.fieldType = type;
this.loader = $.requireNotNull(loader);
this.stringValueLoader = stringValueLoader;
this.converter = converter;
}
示例17
private void registerBuiltInConverters() {
for (Class<? extends Number> numberClass: N.NUMBER_CLASSES) {
addIntoPath(keyOf(numberClass, Number.class), new $.TypeConverter(numberClass, Number.class) {
@Override
public Object convert(Object o) {
return o;
}
});
addIntoPath(keyOf(numberClass, String.class), Lang.TypeConverter.ANY_TO_STRING);
}
for (Field field : $.TypeConverter.class.getFields()) {
if ($.TypeConverter.class.isAssignableFrom(field.getType())) {
try {
$.TypeConverter converter = $.cast(field.get(null));
register(converter);
} catch (IllegalAccessException e) {
throw E.unexpected(e);
}
}
}
for (final Map.Entry<Class, Object> nullValEntry : NULL_VALS.entrySet()) {
register(new $.TypeConverter<Void, Object>(Void.class, nullValEntry.getKey()) {
@Override
public Object convert(Void aVoid) {
return nullValEntry.getValue();
}
});
}
register(NULL_CONVERTER);
}
示例18
PropertyHandlerBase(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
PropertyGetter.NullValuePolicy nullValuePolicy) {
setObjectFactory(objectFactory);
setStringValueResolver(stringValueResolver);
if (null == nullValuePolicy) {
nullValuePolicy = PropertyGetter.NullValuePolicy.RETURN_NULL;
}
setNullValuePolicy(nullValuePolicy);
}
示例19
ListPropertyHandler(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
PropertyGetter.NullValuePolicy nullValuePolicy,
Class<?> itemType) {
super(objectFactory, stringValueResolver, nullValuePolicy);
this.itemType = $.requireNotNull(itemType);
}
示例20
@Override
public <K, V> C.Map<K, V> toMap(Lang.Function<? super T, ? extends K> keyExtractor, Lang.Function<? super T, ? extends V> valExtractor) {
C.Map<K, V> map = C.newMap();
for (T v : this) {
map.put(keyExtractor.apply(v), valExtractor.apply(v));
}
return map;
}
示例21
@Override
public <K> C.Map<K, T> toMapByVal(Lang.Function<? super T, ? extends K> keyExtractor) {
C.Map<K, T> map = C.newMap();
for (T v : this) {
map.put(keyExtractor.apply(v), v);
}
return map;
}
示例22
@Override
public <V> C.Map<T, V> toMapByKey(Lang.Function<? super T, ? extends V> valExtractor) {
C.Map<T, V> map = C.newMap();
for (T v : this) {
map.put(v, valExtractor.apply(v));
}
return map;
}
示例23
MapPropertySetter(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
Class<?> keyType,
Class<?> valType) {
super(objectFactory, stringValueResolver, keyType, valType);
setNullValuePolicy(PropertyGetter.NullValuePolicy.CREATE_NEW);
}
示例24
@Override
public Object apply(String s, Class<?> aClass) throws NotAppliedException, Lang.Break {
StringValueResolver r = resolvers.get(aClass);
if (null != r) {
return r.resolve(s);
}
if (null != s && Enum.class.isAssignableFrom(aClass)) {
return Enum.valueOf(((Class<Enum>) aClass), s);
}
return null;
}
示例25
public MapPropertyHandler(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
Class<?> keyType,
Class<?> valType) {
super(objectFactory, stringValueResolver);
this.keyType = $.requireNotNull(keyType);
this.valType = $.requireNotNull(valType);
}
示例26
public MapPropertyHandler(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
PropertyGetter.NullValuePolicy nullValuePolicy,
Class<?> keyType,
Class<?> valType) {
super(objectFactory, stringValueResolver, nullValuePolicy);
this.keyType = $.requireNotNull(keyType);
this.valType = $.requireNotNull(valType);
}
示例27
public AdaptiveMapPropertyGetter(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
NullValuePolicy nullValuePolicy,
Class<?> keyType,
Class<?> valType) {
super(objectFactory, stringValueResolver, nullValuePolicy, keyType, valType);
}
示例28
AdaptiveMapPropertySetter(Lang.Function<Class<?>, Object> objectFactory,
Lang.Func2<String, Class<?>, ?> stringValueResolver,
Class<?> keyType,
Class<?> valType) {
super(objectFactory, stringValueResolver, keyType, valType);
setNullValuePolicy(PropertyGetter.NullValuePolicy.CREATE_NEW);
}
示例29
@Override
public Object apply(Class<?> aClass) throws NotAppliedException, Lang.Break {
if (List.class.isAssignableFrom(aClass)) {
return C.newList();
} else if (Map.class.isAssignableFrom(aClass)) {
return C.newMap();
}
return Lang.newInstance(aClass);
}
示例30
public static <T> $.Transformer<Iterable<T>, Collection<T>> asCollection() {
return new Lang.Transformer<Iterable<T>, Collection<T>>() {
@Override
public Collection<T> transform(Iterable<T> iterable) {
return C.asCollection(iterable);
}
};
}