Created
June 18, 2020 00:04
-
-
Save mgodave/e10412122ee9172e3cc99dbcb67432b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright [2020] David J. Rusek <[email protected]> | |
* <p> | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* <p> | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* <p> | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.robotninjas.stream; | |
import java.util.Objects; | |
import java.util.function.Function; | |
// wishing I had sealed interfaces here. | |
public abstract class Start { | |
private Start() {} | |
public abstract Offset offset(); | |
public abstract <R> R match( | |
Function<Offset, R> fromNewest, | |
Function<Offset, R> fromOldest, | |
Function<Offset, R> fromOffset | |
); | |
public final static class FromNewest extends Start { | |
private FromNewest() { } | |
@Override | |
public Offset offset() { | |
return Offset.empty(); | |
} | |
@Override | |
public <R> R match( | |
Function<Offset, R> fromNewest, | |
Function<Offset, R> ignore1, | |
Function<Offset, R> ignore2) { | |
return fromNewest.apply(Offset.empty()); | |
} | |
} | |
public final static class FromOldest extends Start { | |
private FromOldest() { } | |
@Override | |
public Offset offset() { | |
return Offset.empty(); | |
} | |
@Override | |
public <R> R match( | |
Function<Offset, R> ignore1, | |
Function<Offset, R> fromOldest, | |
Function<Offset, R> ignore2) { | |
return fromOldest.apply(Offset.empty()); | |
} | |
} | |
public final static class FromOffset extends Start { | |
private final Offset offset; | |
private FromOffset(Offset offset) { | |
this.offset = offset; | |
} | |
@Override | |
public Offset offset() { | |
return offset; | |
} | |
@Override | |
public <R> R match( | |
Function<Offset, R> ignore1, | |
Function<Offset, R> ignore2, | |
Function<Offset, R> fromOffset) { | |
return fromOffset.apply(offset); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof FromOffset that)) return false; | |
return offset.equals(that.offset); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(offset); | |
} | |
} | |
public static FromNewest newest() { | |
return new FromNewest(); | |
} | |
public static FromOldest oldest() { | |
return new FromOldest(); | |
} | |
public static FromOffset offset(Offset offset) { | |
return new FromOffset(offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment