Java Predicates using lifters

Lifters is a technique where we use functions in different context :

Below is lifters in Java creating predicates :

 

package com.z.lift;

public class Person {
	private Double weight;
	private Double height;
	
	
	public Person(Double _weight,Double _height) {
		this.weight = _weight;
		this.height = _height;
	}
	
	public Double getWeight() {
		return weight;
	}
	public Double getHeight() {
		return height;
	}
	
}

package com.z.lift;

public interface PersonProcessor<T> {
	
	T process(Person p);

}

package com.z.lift;

public interface Command<U,V,W> {
	
	 W excute(U u,V v); 

}


package com.z.lift;

public interface PersonLifter<U,V,W> {
	
	public  PersonProcessor<W>  lift (Command<U, V,W> command,
								PersonProcessor<U> personProcessor1,PersonProcessor<V> personProcessor2);

}


package com.z.lift;

public class PersonLifterImpl<U,V,W> implements PersonLifter<U,V,W> {

	@Override
	public  PersonProcessor<W> lift(final Command<U, V, W> command,
			final PersonProcessor<U> personProcessor1,
			final PersonProcessor<V> personProcessor2) {
		
		return new PersonProcessor<W>() {

			@Override
			public W process(Person p) {
				
				return command.excute(personProcessor1.process(p), personProcessor2
						.process(p));
			}
		};
		
	}

}

public class GetConstant<T> implements  PersonProcessor<T>{
	
	
	private Object constant;
	public GetConstant(Object _constant) {
		this.constant = _constant;
	}
	@Override
	public T process(Person p) {
		return (T)constant;
	}
	

}

package com.z.lift;

public class PersonProcessorUtils {
	
	public static PersonProcessor<Boolean> personWeightP(Command<Double, Double, Boolean> command,Double constant) {
		
		return new PersonLifterImpl<Double, Double, Boolean>().lift
					(command, getWeightProcessor(), new GetConstant(constant));
	}
	
	
	
	public static PersonProcessor<Double> getWeightProcessor() {
		
		return new PersonProcessor<Double>() {
			
			@Override
			public Double process(Person p) {
				return p.getWeight();
			}
		};
	}
	
	
	public static  PersonProcessor<Double> getHeightProcessor() {
		
		return new PersonProcessor<Double>() {
			
			@Override
			public Double process(Person p) {
				return p.getHeight();
			}
		};
		
	}
	
	
	public static PersonProcessor<Boolean> personHeightP(Command<Double,Double,Boolean> command,
			Double constant) {
		
		return new PersonLifterImpl<Double, Double, Boolean>().lift
				(command, getHeightProcessor(), new GetConstant(constant));
	}
	
	
	public static PersonProcessor<Boolean> andP (PersonProcessor<Boolean> p1,PersonProcessor<Boolean> p2) {
		
		return new PersonLifterImpl<Boolean, Boolean, Boolean>().lift(
				new Command<Boolean, Boolean, Boolean>() {
					
					@Override
					public Boolean excute(Boolean u, Boolean v) {
						return u && v;
					}
				}, 
				  p1, 
				        p2);
		
	}
	
	
	public static PersonProcessor<Boolean> orP(PersonProcessor<Boolean> p1,PersonProcessor<Boolean> p2) {
		
		return new PersonLifterImpl<Boolean, Boolean, Boolean>().lift(new Command<Boolean, Boolean, Boolean>() {
			
			@Override
			public Boolean excute(Boolean u, Boolean v) {
				return u || v ;
			}
		}, p1, 
		p2);
		
		
		
	}
	
}

//tests 
package com.z.lift;

import static org.junit.Assert.*;

import org.junit.Test;

public class PersonLifterTest {

	
	
	@Test
	public void testConstantP() {
		PersonProcessor<Boolean> constantP = new PersonLifterImpl<Double, Double, Boolean>().lift
					(new Command<Double, Double, Boolean>() {
						
						@Override
						public Boolean excute(Double u, Double v) {
							
							return u > v;
						}
					},new GetConstant<Double>(20d), new GetConstant<Double>(10d));
		
		assertEquals(true, constantP.process(null));
	}
	
	
	@Test
	
	public void testAndPredicate() {
		//weight greater than 20d 
		 PersonProcessor<Boolean> weightGreaterThan20 = PersonProcessorUtils.personWeightP(new Command<Double, Double, Boolean>() {
			
			@Override
			public Boolean excute(Double u, Double v) {
				return u>v;
			}
		}, 20d);
		 
		 assertTrue(weightGreaterThan20.process(new Person(35d,5d)));
		 //weight less than 25d 
		 
		 PersonProcessor<Boolean> heightLesserThan10 = PersonProcessorUtils.personHeightP(new Command<Double, Double, Boolean>() {
			
			@Override
			public Boolean excute(Double u, Double v) {
				return u < v ;
			}
		}, 10d);
		
		 assertTrue(heightLesserThan10.process(new Person(35d, 5d)));
		 
		 
		assertTrue( PersonProcessorUtils.andP(weightGreaterThan20, heightLesserThan10).
				process(new Person(35d, 5d)));
		
		assertFalse(PersonProcessorUtils.andP(weightGreaterThan20, heightLesserThan10).
		process(new Person(35d,11d)));
		
		assertTrue (PersonProcessorUtils.
				orP(weightGreaterThan20, heightLesserThan10).process(new Person(35d, 11d)));
		 
}


Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s