More Related Content Similar to Uwolnić się od "if" Similar to Uwolnić się od "if" (20) Uwolnić się od "if"1. Uwolnić się od if
$flag="OK"; // This is the flag and we set it to OK
http://roflcopter.pl/6025
2. Tomasz Nurkiewicz
5+ lat z Javą
...Scala, JavaScript
SCJP, SCJD, SCWCD, SCBCD
60K+ rep. na StackOverflow
Commiter projektu Logback
nurkiewicz.blogspot.com
scala.net.pl
11. public class Mapper<S extends Comparable<S>, T> {
private final S[] source;
private final T[] target;
// Builder to enable from... to... syntax and
// to make Mapper immutable
public static class Builder<S2 extends Comparable<S2>> {
private final S2[] data;
private Builder(final S2[] data){
this.data = data;
}
public <T2> Mapper<S2, T2> to(final T2... target){
return new Mapper<S2, T2>(this.data, target);
}
}
private Mapper(final S[] source, final T[] target){
final S[] copy = Arrays.copyOf(source, source.length);
Arrays.sort(copy);
this.source = copy;
this.target = Arrays.copyOf(target, target.length);
}
// Factory method to get builder
public static <U extends Comparable<U>, V> Builder<U> from(final U... items){
return new Builder<U>(items);
}
// Map a collection of items
public Collection<T> mapAll(final Collection<? extends S> input){
final Collection<T> output = new ArrayList<T>(input.size());
for(final S s : input){
output.add(this.map(s));
}
return output;
}
// map a single item
public T map(final S input){
final int sourceOffset = Arrays.binarySearch(this.source, input);
return this.target[
Math.min(
this.target.length1,
sourceOffset < 0 ? Math.abs(sourceOffset)2:sourceOffset
)
];
}
}