org.osgl.Lang Java Examples

The following examples show how to use org.osgl.Lang. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginAdmin.java    From actframework with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: EnumLookupCache.java    From actframework with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: Img.java    From java-tool with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
        }
    });
}
 
Example #4
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #6
Source File: SObject.java    From java-tool with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #7
Source File: ListBase.java    From java-tool with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: SysUtilAdmin.java    From actframework with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #9
Source File: App.java    From actframework with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #10
Source File: BeanSpec.java    From java-di with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #11
Source File: C.java    From java-tool with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a function that preppend specified element to argument sequence
 * @param element the element to be added to the head of the argument (sequence type)
 * @param <T> the element type
 * @return the function as described
 * @see Sequence#prepend(Object)
 * @see #prependTo(Sequence)
 */
@SuppressWarnings("unused")
public static <T> $.Processor<Sequence<? super T>> sequencePrepend(final T element) {
    return new Lang.Processor<Sequence<? super T>>() {
        @Override
        public void process(Sequence<? super T> sequence) throws Lang.Break, NotAppliedException {
            sequence.prepend(element);
        }
    };
}
 
Example #12
Source File: ReflectionPropertyGetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object getProperty(Object entity) throws NotAppliedException, Lang.Break {
    if (null == entity) {
        return null;
    }
    ensureMethodOrField(entity);
    try {
        Object v;
        if (null != m) {
            v = m.invoke(entity);
        } else {
            v = f.get(entity);
        }
        if (null == v) {
            switch (nullValuePolicy) {
                case NPE:
                    throw new NullPointerException();
                case CREATE_NEW:
                    v = objectFactory.apply(getPropertyClass(entity));
                    PropertySetter setter = setter();
                    setter.set(entity, v, null);
                    return v;
                default:
                    return null;
            }
        }
        return v;
    } catch (Exception e) {
        throw E.unexpected(e);
    }
}
 
Example #13
Source File: ReflectionPropertySetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
private void setProperty(Object entity, Object value) throws NotAppliedException, Lang.Break {
    if (null == entity) {
        return;
    }
    ensureMethodOrField(entity);
    try {
        doSet(entity, value);
    } catch (Exception e) {
        throw E.unexpected(e);
    }
}
 
Example #14
Source File: MapPropertyGetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
public MapPropertyGetter(Lang.Function<Class<?>, Object> objectFactory,
                         Lang.Func2<String, Class<?>, ?> stringValueResolver,
                         PropertyGetter.NullValuePolicy nullValuePolicy,
                         Class<?> keyType,
                         Class<?> valType) {
    super(objectFactory, stringValueResolver, nullValuePolicy, keyType, valType);
}
 
Example #15
Source File: ReflectionPropertyHandler.java    From java-tool with Apache License 2.0 5 votes vote down vote up
ReflectionPropertyHandler(Lang.Function<Class<?>, Object> objectFactory,
                          Lang.Func2<String, Class<?>, ?> stringValueResolver,
                          PropertyGetter.NullValuePolicy nullValuePolicy,
                          Class entityClass, Method m, Field f) {
    super(objectFactory, stringValueResolver, nullValuePolicy);
    init(entityClass, m, f);
}
 
Example #16
Source File: TypeConverterRegistryTest.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewTypeConverterRegistry() {
    TypeConverterRegistry registry = new TypeConverterRegistry();
    Lang.TypeConverter<Foo, String> converter = registry.get(Foo.class, String.class);
    requireNotNull(converter);
    Foo foo = new Foo();
    eq(S.wrap(foo.id).with(S.BRACKETS), converter.convert(foo));
}
 
Example #17
Source File: StrTestBase.java    From java-tool with Apache License 2.0 5 votes vote down vote up
private static $.Predicate<Character> charIsIn(final char ... ca) {
    return new Lang.Predicate<Character>() {
        @Override
        public boolean test(Character character) {
            for (char c: ca) {
                if (character == c) return true;
            }
            return false;
        }
    };
}
 
Example #18
Source File: ReflectionPropertyGetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
public ReflectionPropertyGetter(Lang.Function<Class<?>, Object> objectFactory,
                                Lang.Func2<String, Class<?>, ?> stringValueResolver,
                                NullValuePolicy nullValuePolicy,
                                Class entityClass, Method m, Field f,
                                ReflectionPropertyHandlerFactory factory) {
    super(objectFactory, stringValueResolver, nullValuePolicy, entityClass, m, f);
    this.factory = $.requireNotNull(factory);
}
 
Example #19
Source File: SimpleObjectFactory.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: AdaptiveMapPropertySetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #21
Source File: AdaptiveMapPropertyGetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
public AdaptiveMapPropertyGetter(Lang.Function<Class<?>, Object> objectFactory,
                                 Lang.Func2<String, Class<?>, ?> stringValueResolver,
                                 NullValuePolicy nullValuePolicy,
                                 Class<?> keyType,
                                 Class<?> valType) {
    super(objectFactory, stringValueResolver, nullValuePolicy, keyType, valType);
}
 
Example #22
Source File: MapPropertyHandler.java    From java-tool with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #23
Source File: MapPropertyHandler.java    From java-tool with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #24
Source File: SimpleStringValueResolver.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #25
Source File: ListPropertyHandler.java    From java-tool with Apache License 2.0 5 votes vote down vote up
ListPropertyHandler(Lang.Function<Class<?>, Object> objectFactory,
                    Lang.Func2<String, Class<?>, ?> stringValueResolver,
                    PropertyGetter.NullValuePolicy nullValuePolicy,
                    Class<?> itemType) {
    super(objectFactory, stringValueResolver, nullValuePolicy);
    this.itemType = $.requireNotNull(itemType);
}
 
Example #26
Source File: StrTestBase.java    From java-tool with Apache License 2.0 5 votes vote down vote up
private static $.Predicate<Character> charIsNotIn(final char ... ca) {
    return new Lang.Predicate<Character>() {
        @Override
        public boolean test(Character character) {
            for (char c: ca) {
                if (character == c) return false;
            }
            return true;
        }
    };
}
 
Example #27
Source File: MapPropertySetter.java    From java-tool with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #28
Source File: ListBase.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #29
Source File: ListBase.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: ListBase.java    From java-tool with Apache License 2.0 5 votes vote down vote up
@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;
}