Skip to content

Commit 5850631

Browse files
committed
Initialize types module with TypeToken wrapper
1 parent a82fd4e commit 5850631

6 files changed

Lines changed: 262 additions & 3 deletions

File tree

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
target/
21
.settings
3-
.classpath
4-
.project
52
.DS_Store
63

74
/.idea
85
*.iml
6+
.classpath
7+
.project
98

109
.java-version
1110

11+
target/
12+
dependency-reduced-pom.xml

core/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<name>DataStax Java driver for Apache Cassandra® - core</name>
3333

3434
<dependencies>
35+
<dependency>
36+
<groupId>${project.groupId}</groupId>
37+
<artifactId>java-driver-types</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
3540
<dependency>
3641
<groupId>com.datastax.oss</groupId>
3742
<artifactId>native-protocol</artifactId>
@@ -73,4 +78,32 @@
7378
<scope>test</scope>
7479
</dependency>
7580
</dependencies>
81+
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<artifactId>maven-shade-plugin</artifactId>
86+
<executions>
87+
<execution>
88+
<goals>
89+
<goal>shade</goal>
90+
</goals>
91+
<configuration>
92+
<artifactSet>
93+
<includes>
94+
<include>com.google.guava:guava</include>
95+
</includes>
96+
</artifactSet>
97+
<relocations>
98+
<relocation>
99+
<pattern>com.google</pattern>
100+
<shadedPattern>com.datastax.oss.driver.shaded.guava</shadedPattern>
101+
</relocation>
102+
</relocations>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
</plugins>
108+
</build>
76109
</project>

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<inceptionYear>2017</inceptionYear>
3535

3636
<modules>
37+
<module>types</module>
3738
<module>core</module>
3839
</modules>
3940

@@ -117,6 +118,10 @@
117118
<artifactId>maven-javadoc-plugin</artifactId>
118119
<version>2.10.4</version>
119120
</plugin>
121+
<plugin>
122+
<artifactId>maven-shade-plugin</artifactId>
123+
<version>3.0.0</version>
124+
</plugin>
120125
<!-- See console.scala in the submodules -->
121126
<plugin>
122127
<groupId>net.alchim31.maven</groupId>

types/pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!--
2+
3+
Copyright (C) 2017-2017 DataStax Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>com.datastax.oss</groupId>
25+
<artifactId>java-driver-parent</artifactId>
26+
<version>4.0.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>java-driver-types</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<name>DataStax Java driver for Apache Cassandra® - data types</name>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.google.guava</groupId>
37+
<artifactId>guava</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.slf4j</groupId>
41+
<artifactId>slf4j-api</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>ch.qos.logback</groupId>
45+
<artifactId>logback-classic</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.testng</groupId>
50+
<artifactId>testng</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.assertj</groupId>
55+
<artifactId>assertj-core</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.mockito</groupId>
60+
<artifactId>mockito-core</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<artifactId>maven-shade-plugin</artifactId>
69+
<executions>
70+
<execution>
71+
<goals>
72+
<goal>shade</goal>
73+
</goals>
74+
<configuration>
75+
<artifactSet>
76+
<includes>
77+
<include>com.google.guava:guava</include>
78+
</includes>
79+
</artifactSet>
80+
<relocations>
81+
<relocation>
82+
<pattern>com.google</pattern>
83+
<shadedPattern>com.datastax.oss.driver.shaded.guava</shadedPattern>
84+
</relocation>
85+
</relocations>
86+
</configuration>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2017-2017 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.types.reflect;
17+
18+
import com.google.common.reflect.TypeToken;
19+
20+
/**
21+
* Runtime representation of a generic Java type.
22+
*
23+
* <p>This is used by type codecs to indicate which Java types they accept, and by generic getters
24+
* and setters in the driver's query API.
25+
*
26+
* <p>To create an instance, use one of the static factory methods, or create an anonymous class:
27+
*
28+
* <pre>{@code
29+
* GenericType<Foo<Bar>> fooBarType = new GenericType<Foo<Bar>>(){};
30+
* }</pre>
31+
*
32+
* You are encouraged to store and reuse these objects.
33+
*/
34+
public abstract class GenericType<T> {
35+
36+
/** Creates a new instance representing a raw Java class. */
37+
public static <T> GenericType<T> of(Class<T> type) {
38+
return new SimpleGenericType<>(type);
39+
}
40+
41+
// This wraps -- and delegates most of the work to -- a Guava type token. The reason we don't
42+
// expose that type directly is because we shade Guava.
43+
private final TypeToken<T> token;
44+
45+
private GenericType(TypeToken<T> token) {
46+
this.token = token;
47+
}
48+
49+
protected GenericType() {
50+
this.token = new TypeToken<T>(getClass()) {};
51+
}
52+
53+
/**
54+
* This method is for internal use, <b>DO NOT use it from client code</b>.
55+
*
56+
* <p>It leaks a shaded type. This should be part of the internal API, but due to internal
57+
* implementation details it has to be exposed here.
58+
*/
59+
public TypeToken<T> __getToken() {
60+
return token;
61+
}
62+
63+
@Override
64+
public boolean equals(Object other) {
65+
if (other == this) {
66+
return true;
67+
} else if (other instanceof GenericType) {
68+
GenericType that = (GenericType) other;
69+
return this.token.equals(that.token);
70+
} else {
71+
return false;
72+
}
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return token.hashCode();
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return token.toString();
83+
}
84+
85+
private static class SimpleGenericType<T> extends GenericType<T> {
86+
SimpleGenericType(Class<T> type) {
87+
super(TypeToken.of(type));
88+
}
89+
}
90+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2017-2017 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.types.reflect;
17+
18+
import com.google.common.reflect.TypeToken;
19+
import java.util.List;
20+
import org.testng.annotations.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class GenericTypeTest {
25+
26+
@Test
27+
public void should_wrap_class() {
28+
GenericType<String> stringType = GenericType.of(String.class);
29+
assertThat(stringType.__getToken()).isEqualTo(TypeToken.of(String.class));
30+
}
31+
32+
@Test
33+
public void should_capture_generic_type() {
34+
GenericType<List<String>> stringListType = new GenericType<List<String>>() {};
35+
TypeToken<List<String>> stringListToken = new TypeToken<List<String>>() {};
36+
assertThat(stringListType.__getToken()).isEqualTo(stringListToken);
37+
}
38+
}

0 commit comments

Comments
 (0)