Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
doujutun3207
flink
提交
3e9d33ee
F
flink
项目概览
doujutun3207
/
flink
与 Fork 源项目一致
从无法访问的项目Fork
通知
24
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
flink
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3e9d33ee
编写于
11月 28, 2015
作者:
G
Gyula Fora
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[FLINK-3075] Change Either creation method names and expose Right/Left classes
Closes #1402
上级
31a2de86
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
82 addition
and
37 deletion
+82
-37
flink-java/src/main/java/org/apache/flink/api/java/typeutils/Either.java
...main/java/org/apache/flink/api/java/typeutils/Either.java
+48
-10
flink-java/src/main/java/org/apache/flink/api/java/typeutils/runtime/EitherSerializer.java
...he/flink/api/java/typeutils/runtime/EitherSerializer.java
+14
-11
flink-java/src/test/java/org/apache/flink/api/java/type/extractor/TypeExtractorTest.java
...ache/flink/api/java/type/extractor/TypeExtractorTest.java
+1
-1
flink-java/src/test/java/org/apache/flink/api/java/typeutils/EitherTypeInfoTest.java
...g/apache/flink/api/java/typeutils/EitherTypeInfoTest.java
+7
-5
flink-java/src/test/java/org/apache/flink/api/java/typeutils/runtime/EitherSerializerTest.java
...link/api/java/typeutils/runtime/EitherSerializerTest.java
+12
-10
未找到文件。
flink-java/src/main/java/org/apache/flink/api/java/typeutils/Either.java
浏览文件 @
3e9d33ee
...
...
@@ -19,39 +19,45 @@
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.
* 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
* @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
>
l
eft
(
L
value
)
{
public
static
<
L
,
R
>
Either
<
L
,
R
>
L
eft
(
L
value
)
{
return
new
Left
<
L
,
R
>(
value
);
}
/**
* Create a Right value of Either
*/
public
static
<
L
,
R
>
Either
<
L
,
R
>
r
ight
(
R
value
)
{
public
static
<
L
,
R
>
Either
<
L
,
R
>
R
ight
(
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
* @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
* @throws IllegalStateException
* if called on a Left
*/
public
abstract
R
right
()
throws
IllegalStateException
;
...
...
@@ -71,7 +77,15 @@ public abstract class Either<L, R> {
return
getClass
()
==
Right
.
class
;
}
private
static
class
Left
<
L
,
R
>
extends
Either
<
L
,
R
>
{
/**
* A left value of {@link Either}
*
* @param <L>
* the type of Left
* @param <R>
* the type of Right
*/
public
static
class
Left
<
L
,
R
>
extends
Either
<
L
,
R
>
{
private
final
L
value
;
public
Left
(
L
value
)
{
...
...
@@ -106,9 +120,25 @@ public abstract class Either<L, R> {
public
String
toString
()
{
return
"Left("
+
value
.
toString
()
+
")"
;
}
/**
* Creates a left value of {@link Either}
*
*/
public
static
<
L
,
R
>
Left
<
L
,
R
>
of
(
L
left
)
{
return
new
Left
<
L
,
R
>(
left
);
}
}
private
static
class
Right
<
L
,
R
>
extends
Either
<
L
,
R
>
{
/**
* A right value of {@link Either}
*
* @param <L>
* the type of Left
* @param <R>
* the type of Right
*/
public
static
class
Right
<
L
,
R
>
extends
Either
<
L
,
R
>
{
private
final
R
value
;
public
Right
(
R
value
)
{
...
...
@@ -143,5 +173,13 @@ public abstract class Either<L, R> {
public
String
toString
()
{
return
"Right("
+
value
.
toString
()
+
")"
;
}
/**
* Creates a right value of {@link Either}
*
*/
public
static
<
L
,
R
>
Right
<
L
,
R
>
of
(
R
right
)
{
return
new
Right
<
L
,
R
>(
right
);
}
}
}
flink-java/src/main/java/org/apache/flink/api/java/typeutils/runtime/EitherSerializer.java
浏览文件 @
3e9d33ee
...
...
@@ -18,6 +18,9 @@
package
org.apache.flink.api.java.typeutils.runtime
;
import
static
org
.
apache
.
flink
.
api
.
java
.
typeutils
.
Either
.
Left
;
import
static
org
.
apache
.
flink
.
api
.
java
.
typeutils
.
Either
.
Right
;
import
java.io.IOException
;
import
org.apache.flink.api.common.typeutils.TypeSerializer
;
...
...
@@ -67,7 +70,7 @@ public class EitherSerializer<L, R> extends TypeSerializer<Either<L, R>> {
@Override
public
Either
<
L
,
R
>
createInstance
()
{
// We arbitrarily always create a Right value instance.
return
Either
.
r
ight
(
rightSerializer
.
createInstance
());
return
R
ight
(
rightSerializer
.
createInstance
());
}
@Override
...
...
@@ -75,12 +78,12 @@ public class EitherSerializer<L, R> extends TypeSerializer<Either<L, R>> {
if
(
from
.
isLeft
())
{
L
left
=
from
.
left
();
L
copyLeft
=
leftSerializer
.
copy
(
left
);
return
Either
.
l
eft
(
copyLeft
);
return
L
eft
(
copyLeft
);
}
else
{
R
right
=
from
.
right
();
R
copyRight
=
rightSerializer
.
copy
(
right
);
return
Either
.
r
ight
(
copyRight
);
return
R
ight
(
copyRight
);
}
}
...
...
@@ -90,19 +93,19 @@ public class EitherSerializer<L, R> extends TypeSerializer<Either<L, R>> {
final
R
right
=
from
.
right
();
if
(
reuse
.
isRight
())
{
R
copyRight
=
rightSerializer
.
copy
(
right
,
reuse
.
right
());
return
Either
.
r
ight
(
copyRight
);
return
R
ight
(
copyRight
);
}
else
{
// if the reuse record isn't a right value, we cannot reuse
R
copyRight
=
rightSerializer
.
copy
(
right
);
return
Either
.
r
ight
(
copyRight
);
return
R
ight
(
copyRight
);
}
}
else
{
L
left
=
from
.
left
();
// reuse record is never a left value because we always create a right instance
L
copyLeft
=
leftSerializer
.
copy
(
left
);
return
Either
.
l
eft
(
copyLeft
);
return
L
eft
(
copyLeft
);
}
}
...
...
@@ -127,10 +130,10 @@ public class EitherSerializer<L, R> extends TypeSerializer<Either<L, R>> {
public
Either
<
L
,
R
>
deserialize
(
DataInputView
source
)
throws
IOException
{
boolean
isLeft
=
source
.
readBoolean
();
if
(
isLeft
)
{
return
Either
.
l
eft
(
leftSerializer
.
deserialize
(
source
));
return
L
eft
(
leftSerializer
.
deserialize
(
source
));
}
else
{
return
Either
.
r
ight
(
rightSerializer
.
deserialize
(
source
));
return
R
ight
(
rightSerializer
.
deserialize
(
source
));
}
}
...
...
@@ -139,16 +142,16 @@ public class EitherSerializer<L, R> extends TypeSerializer<Either<L, R>> {
boolean
isLeft
=
source
.
readBoolean
();
if
(!
isLeft
)
{
if
(
reuse
.
isRight
())
{
return
Either
.
r
ight
(
rightSerializer
.
deserialize
(
reuse
.
right
(),
source
));
return
R
ight
(
rightSerializer
.
deserialize
(
reuse
.
right
(),
source
));
}
else
{
// if the reuse record isn't a right value, we cannot reuse
return
Either
.
r
ight
(
rightSerializer
.
deserialize
(
source
));
return
R
ight
(
rightSerializer
.
deserialize
(
source
));
}
}
else
{
// reuse record is never a left value because we always create a right instance
return
Either
.
l
eft
(
leftSerializer
.
deserialize
(
source
));
return
L
eft
(
leftSerializer
.
deserialize
(
source
));
}
}
...
...
flink-java/src/test/java/org/apache/flink/api/java/type/extractor/TypeExtractorTest.java
浏览文件 @
3e9d33ee
...
...
@@ -1903,7 +1903,7 @@ public class TypeExtractorTest {
@Test
(
expected
=
InvalidTypesException
.
class
)
public
void
testEitherFromObjectException
()
{
Either
<
String
,
Tuple1
<
Integer
>>
either
=
Either
.
l
eft
(
"test"
);
Either
<
String
,
Tuple1
<
Integer
>>
either
=
Either
.
L
eft
(
"test"
);
TypeExtractor
.
getForObject
(
either
);
}
}
flink-java/src/test/java/org/apache/flink/api/java/typeutils/EitherTypeInfoTest.java
浏览文件 @
3e9d33ee
...
...
@@ -18,18 +18,20 @@
package
org.apache.flink.api.java.typeutils
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotEquals
;
import
org.apache.flink.api.common.typeinfo.BasicTypeInfo
;
import
org.apache.flink.api.java.tuple.Tuple2
;
import
org.apache.flink.api.java.typeutils.Either.Right
;
import
org.apache.flink.util.TestLogger
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
public
class
EitherTypeInfoTest
extends
TestLogger
{
Either
<
Integer
,
String
>
intEither
=
Either
.
l
eft
(
1
);
Either
<
Integer
,
String
>
stringEither
=
Either
.
r
ight
(
"boo"
);
Either
<
Integer
,
Tuple2
<
Double
,
Long
>>
tuple2Either
=
Either
.
right
(
new
Tuple2
<
Double
,
Long
>(
42.0
,
2
l
));
Either
<
Integer
,
String
>
intEither
=
Either
.
L
eft
(
1
);
Either
<
Integer
,
String
>
stringEither
=
Either
.
R
ight
(
"boo"
);
Either
<
Integer
,
Tuple2
<
Double
,
Long
>>
tuple2Either
=
new
Right
<>
(
new
Tuple2
<
Double
,
Long
>(
42.0
,
2
l
));
@Test
public
void
testEitherTypeEquality
()
{
...
...
flink-java/src/test/java/org/apache/flink/api/java/typeutils/runtime/EitherSerializerTest.java
浏览文件 @
3e9d33ee
...
...
@@ -20,6 +20,8 @@ package org.apache.flink.api.java.typeutils.runtime;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
apache
.
flink
.
api
.
java
.
typeutils
.
Either
.
Left
;
import
static
org
.
apache
.
flink
.
api
.
java
.
typeutils
.
Either
.
Right
;
import
org.apache.flink.api.common.ExecutionConfig
;
import
org.apache.flink.api.common.typeinfo.BasicTypeInfo
;
...
...
@@ -38,11 +40,11 @@ public class EitherSerializerTest {
public
void
testStringDoubleEither
()
{
Either
<
String
,
Double
>[]
testData
=
new
Either
[]
{
Either
.
l
eft
(
"banana"
),
Either
.
l
eft
(
""
),
Either
.
r
ight
(
32.0
),
Either
.
r
ight
(
Double
.
MIN_VALUE
),
Either
.
r
ight
(
Double
.
MAX_VALUE
)};
L
eft
(
"banana"
),
L
eft
(
""
),
R
ight
(
32.0
),
R
ight
(
Double
.
MIN_VALUE
),
R
ight
(
Double
.
MAX_VALUE
)};
EitherTypeInfo
<
String
,
Double
>
eitherTypeInfo
=
(
EitherTypeInfo
<
String
,
Double
>)
new
EitherTypeInfo
<
String
,
Double
>(
BasicTypeInfo
.
STRING_TYPE_INFO
,
BasicTypeInfo
.
DOUBLE_TYPE_INFO
);
...
...
@@ -58,11 +60,11 @@ public class EitherSerializerTest {
public
void
testEitherWithTuple
()
{
Either
<
Tuple2
<
Long
,
Long
>,
Double
>[]
testData
=
new
Either
[]
{
Either
.
l
eft
(
new
Tuple2
<>(
2
l
,
9
l
)),
Either
.
left
(
new
Tuple2
<>(
Long
.
MIN_VALUE
,
Long
.
MAX_VALUE
)),
Either
.
right
(
32.0
),
Either
.
r
ight
(
Double
.
MIN_VALUE
),
Either
.
r
ight
(
Double
.
MAX_VALUE
)};
Either
.
L
eft
(
new
Tuple2
<>(
2
l
,
9
l
)),
new
Left
<>
(
new
Tuple2
<>(
Long
.
MIN_VALUE
,
Long
.
MAX_VALUE
)),
new
Right
<>
(
32.0
),
R
ight
(
Double
.
MIN_VALUE
),
R
ight
(
Double
.
MAX_VALUE
)};
EitherTypeInfo
<
Tuple2
<
Long
,
Long
>,
Double
>
eitherTypeInfo
=
(
EitherTypeInfo
<
Tuple2
<
Long
,
Long
>,
Double
>)
new
EitherTypeInfo
<
Tuple2
<
Long
,
Long
>,
Double
>(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录