Skip to content

Commit

Permalink
Flesh out base API for data types
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Apr 21, 2017
1 parent 5850631 commit 1045e68
Show file tree
Hide file tree
Showing 20 changed files with 799 additions and 1 deletion.
4 changes: 4 additions & 0 deletions types/pom.xml
Expand Up @@ -32,6 +32,10 @@
<name>DataStax Java driver for Apache Cassandra® - data types</name>

<dependencies>
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>native-protocol</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

public interface CustomType {
/**
* The fully qualified name of the subtype of {@code org.apache.cassandra.db.marshal.AbstractType}
* that represents this type server-side.
*/
String getClassName();
}
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

/**
* The type of a CQL column or function argument.
*
* @see DataTypes
*/
public interface DataType {}
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import com.datastax.oss.driver.internal.types.DefaultCustomType;
import com.datastax.oss.driver.internal.types.DefaultListType;
import com.datastax.oss.driver.internal.types.DefaultMapType;
import com.datastax.oss.driver.internal.types.DefaultSetType;
import com.datastax.oss.driver.internal.types.DefaultTupleType;
import com.datastax.oss.driver.internal.types.PrimitiveType;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;

/** Utility class to get or build {@link DataType} instances. */
public class DataTypes {

public static final DataType ASCII = new PrimitiveType(ProtocolConstants.DataType.ASCII);
public static final DataType BIGINT = new PrimitiveType(ProtocolConstants.DataType.BIGINT);
public static final DataType BLOB = new PrimitiveType(ProtocolConstants.DataType.BLOB);
public static final DataType BOOLEAN = new PrimitiveType(ProtocolConstants.DataType.BOOLEAN);
public static final DataType COUNTER = new PrimitiveType(ProtocolConstants.DataType.COUNTER);
public static final DataType DECIMAL = new PrimitiveType(ProtocolConstants.DataType.DECIMAL);
public static final DataType DOUBLE = new PrimitiveType(ProtocolConstants.DataType.DOUBLE);
public static final DataType FLOAT = new PrimitiveType(ProtocolConstants.DataType.FLOAT);
public static final DataType INT = new PrimitiveType(ProtocolConstants.DataType.INT);
public static final DataType TIMESTAMP = new PrimitiveType(ProtocolConstants.DataType.TIMESTAMP);
public static final DataType UUID = new PrimitiveType(ProtocolConstants.DataType.UUID);
public static final DataType VARINT = new PrimitiveType(ProtocolConstants.DataType.VARINT);
public static final DataType TIMEUUID = new PrimitiveType(ProtocolConstants.DataType.TIMEUUID);
public static final DataType INET = new PrimitiveType(ProtocolConstants.DataType.INET);
public static final DataType DATE = new PrimitiveType(ProtocolConstants.DataType.DATE);
public static final DataType TEXT = new PrimitiveType(ProtocolConstants.DataType.VARCHAR);
public static final DataType TIME = new PrimitiveType(ProtocolConstants.DataType.TIME);
public static final DataType SMALLINT = new PrimitiveType(ProtocolConstants.DataType.SMALLINT);
public static final DataType TINYINT = new PrimitiveType(ProtocolConstants.DataType.TINYINT);
public static final DataType DURATION = new PrimitiveType(ProtocolConstants.DataType.DURATION);

public static CustomType custom(String className) {
return new DefaultCustomType(className);
}

public static ListType listOf(DataType elementType) {
// frozen == true is only used in column definitions, it's unlikely that end users will need to
// create such instances.
return new DefaultListType(elementType, false);
}

public static SetType setOf(DataType elementType) {
return new DefaultSetType(elementType, false);
}

public static MapType mapOf(DataType keyType, DataType valueType) {
return new DefaultMapType(keyType, valueType, false);
}

public static TupleType tupleOf(DataType... componentTypes) {
return new DefaultTupleType(ImmutableList.copyOf(Arrays.asList(componentTypes)));
}
}
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import com.datastax.oss.driver.api.types.DataType;

public interface ListType {
public DataType getElementType();

public boolean isFrozen();
}
26 changes: 26 additions & 0 deletions types/src/main/java/com/datastax/oss/driver/api/types/MapType.java
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import com.datastax.oss.driver.api.types.DataType;

public interface MapType {
public DataType getKeyType();

public DataType getValueType();

public boolean isFrozen();
}
24 changes: 24 additions & 0 deletions types/src/main/java/com/datastax/oss/driver/api/types/SetType.java
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import com.datastax.oss.driver.api.types.DataType;

public interface SetType {
public DataType getElementType();

public boolean isFrozen();
}
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import java.util.List;

public interface TupleType {
List<DataType> getComponentTypes();
}
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.api.types;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import java.util.Map;

public interface UserDefinedType {
CqlIdentifier getKeyspace();

CqlIdentifier getName();

Map<CqlIdentifier, DataType> getFieldTypes();
}
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.internal.types;

import com.datastax.oss.driver.api.types.CustomType;
import com.google.common.base.Preconditions;

public class DefaultCustomType implements CustomType {
private final String className;

public DefaultCustomType(String className) {
Preconditions.checkNotNull(className);
this.className = className;
}

@Override
public String getClassName() {
return className;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
} else if (other instanceof CustomType) {
CustomType that = (CustomType) other;
return this.className.equals(that.getClassName());
} else {
return false;
}
}

@Override
public int hashCode() {
return className.hashCode();
}

@Override
public String toString() {
return "Custom(" + className + ")";
}
}
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017-2017 DataStax Inc.
*
* 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
*
* http://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 com.datastax.oss.driver.internal.types;

import com.datastax.oss.driver.api.types.DataType;
import com.datastax.oss.driver.api.types.ListType;
import com.google.common.base.Preconditions;

public class DefaultListType implements ListType {
private final DataType elementType;
private final boolean frozen;

public DefaultListType(DataType elementType, boolean frozen) {
Preconditions.checkNotNull(elementType);
this.elementType = elementType;
this.frozen = frozen;
}

@Override
public DataType getElementType() {
return elementType;
}

@Override
public boolean isFrozen() {
return frozen;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
} else if (other instanceof ListType) {
ListType that = (ListType) other;
// frozen is not taken into account
return this.elementType.equals(that.getElementType());
} else {
return false;
}
}

@Override
public int hashCode() {
return this.elementType.hashCode();
}

@Override
public String toString() {
return "List(" + elementType + ", " + (frozen ? "" : "not ") + "frozen)";
}
}

0 comments on commit 1045e68

Please sign in to comment.