PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/de/jbee/lang/Operator.java

http://github.com/jbee/jadamant
Java | 162 lines | 124 code | 38 blank | 0 comment | 4 complexity | 1e0607cb27f6b25127089312c3751537 MD5 | raw file
  1package de.jbee.lang;
  2
  3import de.jbee.lang.dev.Null;
  4import de.jbee.lang.dev.Nullproof;
  5import de.jbee.lang.dev.Nullsave;
  6
  7public final class Operator {
  8
  9	public static final Op<Object> minimum = minimumBy( Order.inherent );
 10	public static final Op<Object> maximim = maximumBy( Order.inherent );
 11	public static final Op<Object> elvis = new ElvisOp<Object>();
 12
 13	private Operator() {
 14		throw new UnsupportedOperationException( "util" );
 15	}
 16
 17	public static <T> Op<T> maximumBy( Ord<T> order ) {
 18		return order == Order.inherent
 19			? Cast.<T> genericDowncast( maximim )
 20			: new MaximumOp<T>( order );
 21	}
 22
 23	public static <T> Op<T> minimumBy( Ord<T> order ) {
 24		return order == Order.inherent
 25			? Cast.<T> genericDowncast( minimum )
 26			: new MinimumOp<T>( order );
 27	}
 28
 29	public static <T> RelationalOp<T> not( RelationalOp<T> op ) {
 30		return op instanceof NegateRelationalOp<?>
 31			? ( (NegateRelationalOp<T>) op ).op
 32			: new NegateRelationalOp<T>( op );
 33	}
 34
 35	public static <T> RelationalOp<T> ltBy( Ord<? super T> order ) {
 36		return new OrderRelationalOp<T>( order, Ordering.GT );
 37	}
 38
 39	public static <T> RelationalOp<T> gtBy( Ord<? super T> order ) {
 40		return new OrderRelationalOp<T>( order, Ordering.LT );
 41	}
 42
 43	public static <T> RelationalOp<T> eqBy( Ord<? super T> order ) {
 44		return new OrderRelationalOp<T>( order, Ordering.EQ );
 45	}
 46
 47	public static <T> RelationalOp<T> notEqBy( Ord<? super T> order ) {
 48		return not( eqBy( order ) );
 49	}
 50
 51	public static <T> RelationalOp<T> geBy( Ord<? super T> order ) {
 52		return not( ltBy( order ) );
 53	}
 54
 55	public static <T> RelationalOp<T> leBy( Ord<? super T> order ) {
 56		return not( gtBy( order ) );
 57	}
 58
 59	static final class ElvisOp<T>
 60			implements Op<T>, Nullsave {
 61
 62		@Override
 63		public T operate( T left, T right ) {
 64			return left == null
 65				? right
 66				: left;
 67		}
 68
 69	}
 70
 71	static final class MaximumOp<T>
 72			implements Op<T>, Nullproof {
 73
 74		private final Ord<T> ord;
 75
 76		MaximumOp( Ord<T> ord ) {
 77			super();
 78			this.ord = ord;
 79		}
 80
 81		@Override
 82		public T operate( T left, T right ) {
 83			return ord.ord( left, right ).isGt()
 84				? left
 85				: right;
 86		}
 87
 88		@Override
 89		public boolean isNullsave() {
 90			return Null.isSave( ord );
 91		}
 92
 93	}
 94
 95	static final class MinimumOp<T>
 96			implements Op<T>, Nullproof {
 97
 98		private final Ord<T> ord;
 99
100		MinimumOp( Ord<T> ord ) {
101			super();
102			this.ord = ord;
103		}
104
105		@Override
106		public T operate( T left, T right ) {
107			return ord.ord( left, right ).isLe()
108				? left
109				: right;
110		}
111
112		@Override
113		public boolean isNullsave() {
114			return Null.isSave( ord );
115		}
116	}
117
118	static final class OrderRelationalOp<T>
119			implements RelationalOp<T>, Nullproof {
120
121		private final Ord<? super T> ord;
122		private final Ordering expected;
123
124		OrderRelationalOp( Ord<? super T> ord, Ordering expected ) {
125			super();
126			this.ord = ord;
127			this.expected = expected;
128		}
129
130		@Override
131		public boolean holds( T left, T right ) {
132			return ord.ord( left, right ) == expected;
133		}
134
135		@Override
136		public boolean isNullsave() {
137			return Null.isSave( ord );
138		}
139	}
140
141	static final class NegateRelationalOp<T>
142			implements RelationalOp<T>, Nullproof {
143
144		final RelationalOp<T> op;
145
146		NegateRelationalOp( RelationalOp<T> op ) {
147			super();
148			this.op = op;
149		}
150
151		@Override
152		public boolean holds( T left, T right ) {
153			return !op.holds( left, right );
154		}
155
156		@Override
157		public boolean isNullsave() {
158			return Null.isSave( op );
159		}
160
161	}
162}