aboutsummaryrefslogtreecommitdiff
path: root/Mammoth/Couscous/java/util/Optional.cs
blob: 1ae8e00085e9acb911936891e0afcb3c2db0e86c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Mammoth.Couscous.java.util.function;

namespace Mammoth.Couscous.java.util
{
    internal interface Optional<T>
	{
		bool isPresent();
		Optional<U> map<U>(Function<T, U> function);
		Optional<U> flatMap<U>(Function<T, Optional<U>> function);
		Optional<T> filter(Predicate<T> predicate);
		T orElse(T value);
		T orElseGet(Supplier<T> supplier);
		T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception;
		T get();
		void ifPresent(Consumer<T> consumer);
    }
    
    internal interface None {
	}

	internal struct None<T> : None, Optional<T> {
		internal static readonly None<T> Instance = new None<T>();
		
        public override bool Equals(object other) {
			return other is None;
        }
        
        public override int GetHashCode() {
			return 0;
		}
		
		public bool isPresent() {
			return false;
		}

		public Optional<U> map<U>(Function<T, U> function) {
			return new None<U>();
		}

		public Optional<U> flatMap<U>(Function<T, Optional<U>> function) {
			return new None<U>();
		}
		
		public Optional<T> filter(Predicate<T> predicate) {
			return this;
		}

		public T orElse(T value) {
			return value;
		}
		
		public T orElseGet(Supplier<T> supplier) {
			return supplier.get();
		}
		
		public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception {
			throw exceptionSupplier.get();
		}

		public T get() {
			throw new NoSuchElementException();
		}
		
		public void ifPresent(Consumer<T> consumer) {
		}
	}
	
	internal interface Some {
		object getObject();
	}

	internal struct Some<T> : Some, Optional<T> {
		private readonly T _value;

		internal Some(T value) {
			_value = value;
		}
		
        public override bool Equals(object other) {
			var otherSome = other as Some;
			if (otherSome == null) {
				return false;
			} else {
				return _value.Equals(otherSome.getObject());
			}
        }
        
        public override int GetHashCode() {
			return _value.GetHashCode();
		}

		public bool isPresent() {
			return true;
		}

		public Optional<U> map<U>(Function<T, U> function) {
			return new Some<U>(function.apply(_value));
		}

		public Optional<U> flatMap<U>(Function<T, Optional<U>> function) {
			return function.apply(_value);
		}
		
		public Optional<T> filter(Predicate<T> predicate) {
			if (predicate.test(_value)) {
				return this;
			} else {
				return None<T>.Instance;
			}
		}

		public T orElse(T value) {
			return _value;
		}
		
		public T orElseGet(Supplier<T> supplier) {
			return _value;
		}
		
		public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception {
			return _value;
		}

		public T get() {
			return _value;
		}

		public object getObject() {
			return _value;
		}
		
		public void ifPresent(Consumer<T> consumer) {
			consumer.accept(_value);
		}
	}

    internal static class Optional
    {
		internal static Optional<T> empty<T>()
        {
			return None<T>.Instance;
        }
        
        internal static Optional<T> of<T>(T value)
        {
			return new Some<T> (value);
        }
    }
}