forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-3002] Add Either type, EitherTypeInfo, and EitherSerializer to…
… the Java API This closes apache#1371
- Loading branch information
Showing
6 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
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
147 changes: 147 additions & 0 deletions
147
flink-java/src/main/java/org/apache/flink/api/java/typeutils/Either.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.apache.flink.api.java.typeutils; | ||
|
||
/** | ||
* This type represents a value of one two possible types, Left or Right | ||
* (a disjoint union), inspired by Scala's Either type. | ||
* | ||
* @param <L> the type of Left | ||
* @param <R> the type of Right | ||
*/ | ||
public abstract class Either<L, R> { | ||
|
||
/** | ||
* Create a Left value of Either | ||
*/ | ||
public static <L, R> Either<L, R> left(L value) { | ||
return new Left<L, R>(value); | ||
} | ||
|
||
/** | ||
* Create a Right value of Either | ||
*/ | ||
public static <L, R> Either<L, R> right(R value) { | ||
return new Right<L, R>(value); | ||
} | ||
|
||
/** | ||
* Retrieve the Left value of Either. | ||
* @return the Left value | ||
* @throws IllegalStateException if called on a Right | ||
*/ | ||
public abstract L left() throws IllegalStateException; | ||
|
||
/** | ||
* Retrieve the Right value of Either. | ||
* @return the Right value | ||
* @throws IllegalStateException if called on a Left | ||
*/ | ||
public abstract R right() throws IllegalStateException; | ||
|
||
/** | ||
* | ||
* @return true if this is a Left value, false if this is a Right value | ||
*/ | ||
public final boolean isLeft() { | ||
return getClass() == Left.class; | ||
} | ||
|
||
/** | ||
* | ||
* @return true if this is a Right value, false if this is a Left value | ||
*/ | ||
public final boolean isRight() { | ||
return getClass() == Right.class; | ||
} | ||
|
||
private static class Left<L, R> extends Either<L, R> { | ||
private final L value; | ||
|
||
public Left(L value) { | ||
this.value = java.util.Objects.requireNonNull(value); | ||
} | ||
|
||
@Override | ||
public L left() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public R right() { | ||
throw new IllegalStateException("Cannot retrieve Right value on a Left"); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object instanceof Left<?, ?>) { | ||
final Left<?, ?> other = (Left<?, ?>) object; | ||
return value.equals(other.value); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Left(" + value.toString() + ")"; | ||
} | ||
} | ||
|
||
private static class Right<L, R> extends Either<L, R> { | ||
private final R value; | ||
|
||
public Right(R value) { | ||
this.value = java.util.Objects.requireNonNull(value); | ||
} | ||
|
||
@Override | ||
public L left() { | ||
throw new IllegalStateException("Cannot retrieve Left value on a Right"); | ||
} | ||
|
||
@Override | ||
public R right() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object instanceof Right<?, ?>) { | ||
final Right<?, ?> other = (Right<?, ?>) object; | ||
return value.equals(other.value); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Right(" + value.toString() + ")"; | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
flink-java/src/main/java/org/apache/flink/api/java/typeutils/EitherTypeInfo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.apache.flink.api.java.typeutils; | ||
|
||
import org.apache.flink.api.common.ExecutionConfig; | ||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.api.common.typeutils.TypeSerializer; | ||
import org.apache.flink.api.java.typeutils.runtime.EitherSerializer; | ||
|
||
/** | ||
* A {@link TypeInformation} for the {@link Either} type of the Java API. | ||
* | ||
* @param <L> the Left value type | ||
* @param <R> the Right value type | ||
*/ | ||
public class EitherTypeInfo<L, R> extends TypeInformation<Either<L, R>> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final TypeInformation<L> leftType; | ||
|
||
private final TypeInformation<R> rightType; | ||
|
||
public EitherTypeInfo(TypeInformation<L> leftType,TypeInformation<R> rightType) { | ||
this.leftType = leftType; | ||
this.rightType = rightType; | ||
} | ||
|
||
@Override | ||
public boolean isBasicType() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isTupleType() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int getArity() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public int getTotalFields() { | ||
return 1; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<Either<L, R>> getTypeClass() { | ||
return (Class<Either<L, R>>) (Class<?>) Either.class; | ||
} | ||
|
||
@Override | ||
public boolean isKeyType() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public TypeSerializer<Either<L, R>> createSerializer(ExecutionConfig config) { | ||
return new EitherSerializer<L, R>(leftType.createSerializer(config), | ||
rightType.createSerializer(config)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Either <" + leftType.toString() + ", " + rightType.toString() + ">"; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof EitherTypeInfo) { | ||
EitherTypeInfo<L, R> other = (EitherTypeInfo<L, R>) obj; | ||
|
||
return other.canEqual(this) && | ||
leftType.equals(other.leftType) && | ||
rightType.equals(other.rightType); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 17 * leftType.hashCode() + rightType.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean canEqual(Object obj) { | ||
return obj instanceof EitherTypeInfo; | ||
} | ||
|
||
} |
Oops, something went wrong.