All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unit16.z.Pair Maven / Gradle / Ivy

package com.unit16.z;

import java.util.function.Function;

import com.google.common.collect.UnmodifiableIterator;


public interface Pair {
	
	A fst();
	
	B snd();
	
	public static final class S
	{
		public static  Function, X> fstM()
		{
			
			return new Function, X>()
			{
				@Override
				public X apply(Pair input) { return input.fst(); }	
			};
		}
		
		public static  Function, X> sndM()
		{
			
			return new Function, X>()
			{
				@Override
				public X apply(Pair input) { return input.snd(); }	
			};
		}
	}
	
	public static interface IUniform extends Pair {
		C get(boolean flag);
	}
	
	public static class Const implements Pair
	{
		private final C _c;
		private final D _d;
		
		public Const(C c, D d)
		{
			_c = c;
			_d = d;
		}

		@Override public final C fst() { return _c; }

		@Override public final D snd() { return _d; }
		
		public static class Uniform extends Const 
		implements Pair.IUniform, Iterable
		{
			public static  Uniform pair(E c, E d)
			{
				return new Uniform<>(c, d);
			}
			
			protected Uniform(E c, E d) {
				super(c, d);
			}
			
			@Override
			public final E get(boolean isFirst)
			{
				return isFirst ? fst() : snd();
			}

			@Override
			public final UnmodifiableIterator iterator() {
				return new UnmodifiableIterator() {

					int count = 0;
					
					@Override public boolean hasNext() { return count < 2; }

					@Override
					public E next() {
						count++;
						return count == 1 ? fst() : snd();
					}
				};
			}
			
			@Override
			public String toString()
			{
				return "(" + fst() + "," + snd() + "}";
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy