-
Notifications
You must be signed in to change notification settings - Fork 56
Enum On
Ori Roth edited this page Apr 2, 2017
·
3 revisions
public enum On {
/*
* Utilities (5)
*/
static T sign(int selector, T onNegative, T onZero, T onPositive);
static T sign(int selector, Function<T> onNegative, Function<T> onZero, Function<T> onPositive);
static void sign(int selector, Action onNegative, Action onZero, Action onPositive);
static void sign(Integer selector, Action onNegative, Action onZero, Action onPositive);
static void main(String[] args);
/*
* Nested types (2)
*/
abstract static interface Function<T> { ... }
abstract static interface Action { ... }
}
Input types: Action, Function.
public interface On.Function<T> {
/*
* Type (1)
*/
T _();
}
public interface On.Action {
/*
* Type (1)
*/
void _();
}
// SSDLPedia
package il.ac.technion.cs.ssdl.utils;
import il.ac.technion.cs.ssdl.stereotypes.Utility;
/**
* A utility class, providing functions realizing lazy three-way branching,
* depending on the sign of a given integer
*
* Author: Yossi Gil, the Technion.
* See: 24/07/2008
*/
@Utility public enum On {
;
/**
* A non-lazy selection between three values depending on the sign of a
* given integer
*
* <T> type of values from which to select a return value
* selector an integer whose sign determines the returned value
* onNegative value to return if selector is negative
* onZero value to return if selector is zero
* onPositive value to return if selector is positive
* Return: one of onNegative, onZero or
* onPositive, depending on the sign of
* selector
*/
public static <T> T sign(final int selector, final T onNegative, final T onZero, final T onPositive) {
if (selector == 0)
return onZero;
return selector < 0 ? onNegative : onPositive;
}
/**
* A lazy selection between three expressions depending on the sign of a
* given integer. Each expression is given as an instance of a class
* implementing the Function<T>
* interface.
*
* <T> type of values from which to select a return value
* selector an integer whose sign determines the returned value
* onNegative expression to evaluate and return if
* selector is negative
* onZero expression to evaluate and return if selector
* is zero
* onPositive expression to evaluate and return if
* selector is positive
* Return: one of onNegative._(), onZero._() or
* onPositive._(), depending on the sign of
* selector
*/
public static <T> T sign(final int selector, final Function<T> onNegative, final Function<T> onZero,
final Function<T> onPositive) {
if (selector == 0)
return onZero._();
return selector < 0 ? onNegative._() : onPositive._();
}
/**
* Select between one of three actions to carry out, depending on the sign
* of a given integer. Each action is given as an instance of a class
* implementing the Action<T>
* interface.
*
* selector an integer whose sign determines the returned value
* onNegative what to do in case selector is negative
* onZero what to do in case selector is zero
* onPositive what to do in case selector is positive
*/
public static void sign(final int selector, final Action onNegative, final Action onZero, final Action onPositive) {
sign(selector, asFunction(onNegative), asFunction(onZero), asFunction(onPositive));
}
/**
* Select between one of three actions to carry out, depending on the sign
* of a given integer. Each action is given as an instance of a class
* implementing the Action<T>
* interface.
*
* selector an integer whose sign determines the returned value
* onNegative what to do in case selector is negative
* onZero what to do in case selector is zero
* onPositive what to do in case selector is positive
*/
public static void sign(final Integer selector, final Action onNegative, final Action onZero, final Action onPositive) {
sign(selector.intValue(), asFunction(onNegative), asFunction(onZero), asFunction(onPositive));
}
public interface Function<T> {
T _();
}
public interface Action {
void _();
}
private static Function<Void> asFunction(final Action a) {
return new Function<Void>() {
@Override public Void _() {
a._();
return null;
}
};
}
public static void main(final String[] args) {
for (final String arg : args) {
System.out.print("Argument " + arg + " is ");
On.sign(Integer.valueOf(arg), new Action() {
public void _() {
System.out.println("negative!");
}
}, new Action() {
public void _() {
System.out.println("zero!");
}
}, new Action() {
public void _() {
System.out.println("positive!");
}
});
}
}
}
Metric | Value | Acronym | Explanation |
---|---|---|---|
LOC | 125 | Lines Of Code | Total number of lines in the code |
SCC | 19 | SemiColons Count | Total number of semicolon tokens found in the code. |
NOT | 416 | Number Of Tokens | Comments, whitespace and text which cannot be made into a token not included. |
VCC | 3597 | Visible Characters Count | The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters. |
CCC | 1337 | Code Characters Count | Total number of non-white characters in tokens. White space characters in string and character literals are not counted. |
UIC | 32 | Unique Identifiers Count | The number of different identifiers found in the code |
WHC | 5 | Weighted Horizontal Complexity | A heuritistic on horizontal complexity |
Statistic | Value |
---|---|
Average token length | 3.2 |
Tokens/line | 3.3 |
Visible characters/line | 29 |
Code characters/line | 11 |
Semicolons/tokens | 4% |
Comment text percentage | 62% |
Token Kind | Occurrences |
---|---|
KEYWORD | 67 |
OPERATOR | 24 |
LITERAL | 9 |
ID | 133 |
PUNCTUATION | 183 |
COMMENT | 6 |
OTHER | 210 |