A Java library helping you to compose behavior from smaller parts.
Documentation can be found here: http://runeflobakk.github.io/motif
A Java library helping you to compose behavior from smaller parts.
Home Page: http://runeflobakk.github.io/motif
A Java library helping you to compose behavior from smaller parts.
Documentation can be found here: http://runeflobakk.github.io/motif
Fn to get Object.hashCode()
For expensive map and/or filters, it may be convenient to force evaluation and still return Elements for further mapping/filtering/other operations. This is opposed to calling collect() and then wrap the returned List in another Iterate.on(..).
It's already possible to create Comparators using a Fn<?, ? extends Comparable<?>>
. It should return an interface (say EnhancedComparator<T>
or similar) with methods to create modifications based on the original Comparator.
Suggestion:
interface EnhancedComparator<T> extends Comparator<T> {
EnhancedComparator<T> descending();
EnhancedComparator<T> nullsFirst(); //nullsafe comparator
EnhancedComparator<T> nullsLast(); //nullsafe comparator
}
A Fn which takes a String and returns it with its characters in reversed order
Repeat a string a given amount, optionally with a separator String.
Try the markdown doclet mentioned in this blog http://blog.paralleluniverse.co/2014/05/01/modern-java/
<dependency>
<groupId>ch.raffael.pegdown-doclet</groupId>
<artifactId>pegdown-doclet</artifactId>
<version>1.1.1</version>
</dependency>
Implement Strings.substring so that it avoids throwing exceptions. The behavior of Apache Commons Lang's StringUtils.substring is suitable.
Also add a substring that takes just the beginIndex and extends to the end of the string.
Pushed to dev branch 6f2452f
Every String function must accept Locale. In addition, provide a static function the developer can supply once to have Locales resolved implicitly. (Typically fetch it from a thread local Web session or something)
The name change should make it clearer that the operation will insert a string between two other strings, and not extract a substring.
Predicate which wraps Collection.contains(T)
equalTo-predicate which resolves its operand on each application. Typically used to check for equivalence on some independently continuously changing value, e.g. the system clock.
The abstract classes NullIfArgIsNull
and DefaultIfArgIsNull
are not needed anymore, and can be removed. Existing usage can be replaced with composition using Base.when(..)
instead of the slightly awkward inheritance based approach with those classes.
<I, O> When<I, O> when(Predicate<? super I> condition, Fn<I, O> fn)
Not possible to use a more specific Predicate than the mapping Fn.
Result type should be an Fn
.
This method seems more appropriate in the Iterate class, as it deals with iterables/arrays. It should be moved there.
Similar to #7 but the resulting map values are lists, and thus is usable for non-unique computed keys.
The original reason for Motif being compiled for Java 7 is the @SafeVarargs
annotation introduced in Java 7. It has come to my attention that using the @SafeVarargs
annotation, even when it is included in the binary with RetentionPolicy.RUNTIME
, will not break when executed on a platform without the annotation (i.e. Java 6, and Java >= 5.0u6) as per the Java Language Spec
Filtering elements containing nulls will stop at the first null.
Makes it easier to work with. E.g. usable with Optional.each(..).
Map a string to the index of the first appearing character, much the same way as java.lang.String.indexOf.
Should probably return null
instead of -1 for no matches, in order to correctly map an Optional to None.
May also include a variant to find the beginning index of a substring? Strings.indexOf(String)
class Class does not implement Member. Thus Reflect.name only works with methods and fields.
Not really a bug, but definitely a missing feature which was expected to be there.
See if an upcast operation can be implemented on Elements. See #36
Fn<String, Iterable<String>>
which puts all substrings it can find between two given strings in an iterable.
For Iterables which consumes its contents, typically Iterable views of streams, this optimization may yields some surprises, as one element will be consumed during the check for emptiness.
E.g.
public final class ZipEntries implements Iterable<ZipEntry> {
private final ZipInputStream zip;
public ZipEntries(ZipInputStream zip) { this.zip = zip; }
@Override public Iterator<ZipEntry> iterator() {
return new SimpleIterator<ZipEntry>() {
@Override protected Optional<? extends ZipEntry> nextIfAvailable() {
try {
return optional(zip.getNextEntry());
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}};
}
}
will skip over the first element if wrapped with Iterate.on(new ZipEntries(..))
Do<Byte>
which wraps a call to OutputStream.write(int)
It has come to my attention that Motif may use the @SafeVarargs
annotation and still be compatible with Java 6, as the Java Language Specification states that an annotation compiled into the binary (RetentionPolicy.{RUNTIME, CLASS}
) does actually not need to be available at runtime.
So Motif can be developed on Java 7 and compiled for Java 6 as long no Java 7-specific API is used. However, using @SafeVarargs
will not prevent the use of Motif on Java 6.
http://mojo.codehaus.org/animal-sniffer-maven-plugin/examples/generating-java-signatures.html
Create Iterate.on(Map) which returns Elements<Map.Entry>.
Also create Fn implementations to extract key and value from Map.Entry.
Iterate.on(String)
invokes toCharArray
which effectively duplicates the characters of the given string. This is unnecessary.
Implement an abstract Iterator to better support iterating elements given a start and end index. Also provide implementation for iterating characters of a String, and most common arrays of primitives (byte, char, int).
Allow SimpleIterator.nextIfAvailable() to throw checked Exceptions. If implementation throws exception, SimpleIterator rethrows it wrapped in a RuntimeException if original exception is checked.
A Fn to get the name of a java.lang.reflect.Member (class, method, field, etc)
Works as long as the mapping Fn is referentially transparent, but if it has side effects (e.g. printing to console or other IO) this will be performed twice.
Consider fasttrack-releasing fix for this as version 0.4.1.
In most circumstances, None is a true singleton, effectively enforced through standard Java visibility rules. Even so, using Optional with libraries/frameworks which relies on serialization (e.g. Hazelcast, Wicket, etc), will manage to deserialize to a new None-instance, and thus none.equals(anotherNone)
will return false.
The method to override in Do<V>
should be renamed to be able to have classes implementing both Fn<I,O>
and Do<V>
.
Make no.motif.f.base.Throw<I1, I2, O>
implement Do<I1>
when this is done.
The method-name "split" on Optional<V>
and A<V>
is somewhat awkward since it hints about how a value is transformed into several values. The passed Fn
should describe how it will be done, e.g. splittingOn(',')
. Using this as an exsample, possible names can be:
the("a,b,c").explode(splittingOn(','))
the("a,b,c").toElements(splittingOn(','))
the("a,b,c").dissect(splittingOn(','))
Fn
to get size()
of a Collection.
Fn to get the packagename of a class. May be combined as a Fn<Class, Package>
and Fn<Package, String>
.
New "bridge" back to Java collections which are able to on each element, compute a key for the element, and put the resulting key and element as key and value into a map, which is returned. The mapBy method must include documentation that the Fn should compute unique keys for each element, and what the behavior will be if that is not the case.
It is sometimes useful to upcast an Optional to a less specific type
Say if your method is declared to return an Optional<A>
, and your method prepares a value v
of type B extends A
. The statement return optional(v)
will not compile, since the type is inferred to be Optional<B>
Candidate method names:
.as(Class<N super V>): Optional<N>
.upcastTo(Class<N super V>): Optional<N>
Shorten getOrElse() to orElse(), and add orNull() and or(Optional)
Substring operations:
This depends on #10 being done first.
Predicate to identify elements with a specified annotation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.