Skip to content

Commit

Permalink
runtime-config: Separate TypeMapper into its own trait
Browse files Browse the repository at this point in the history
This shouldn't affect the API since the exposed methods and global fields remain the same, they are just located in their own interface that Column now implements.
  • Loading branch information
Pyknic committed Jan 10, 2017
1 parent 705fd2e commit 38929d7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 44 deletions.
Expand Up @@ -16,12 +16,17 @@
*/
package com.speedment.runtime.config;


import com.speedment.runtime.config.exception.SpeedmentConfigException;
import com.speedment.runtime.config.mutator.ColumnMutator;
import com.speedment.runtime.config.mutator.DocumentMutator;
import com.speedment.runtime.config.trait.*;
import static com.speedment.runtime.config.util.DocumentUtil.newNoSuchElementExceptionFor;
import com.speedment.runtime.config.trait.HasAlias;
import com.speedment.runtime.config.trait.HasEnabled;
import com.speedment.runtime.config.trait.HasMainInterface;
import com.speedment.runtime.config.trait.HasMutator;
import com.speedment.runtime.config.trait.HasName;
import com.speedment.runtime.config.trait.HasNullable;
import com.speedment.runtime.config.trait.HasOrdinalPosition;
import com.speedment.runtime.config.trait.HasParent;
import com.speedment.runtime.config.trait.HasTypeMapper;
import java.util.Optional;

/**
Expand All @@ -31,7 +36,6 @@
* @author Emil Forslund
* @since 2.0.0
*/

public interface Column extends
Document,
HasParent<Table>,
Expand All @@ -40,14 +44,12 @@ public interface Column extends
HasAlias,
HasNullable,
HasOrdinalPosition,
HasTypeMapper,
HasMainInterface,
HasMutator<ColumnMutator<? extends Column>> {

String
AUTO_INCREMENT = "autoIncrement",
TYPE_MAPPER = "typeMapper",
DATABASE_TYPE = "databaseType",
ENUM_CONSTANTS = "enumConstants";
String AUTO_INCREMENT = "autoIncrement",
ENUM_CONSTANTS = "enumConstants";

/**
* Returns whether or not this column will auto increment when new values
Expand All @@ -59,25 +61,6 @@ public interface Column extends
default boolean isAutoIncrement() {
return getAsBoolean(AUTO_INCREMENT).orElse(false);
}

/**
* Returns the name of the class that represents the database type.
*
* @return the database type class
*/
default String getDatabaseType() {
return getAsString(DATABASE_TYPE).orElseThrow(newNoSuchElementExceptionFor(this, DATABASE_TYPE));
}

/**
* Returns the name of the mapper class that will be used to generate a java
* representation of the database types.
*
* @return the mapper class
*/
default Optional<String> getTypeMapper() {
return getAsString(TYPE_MAPPER);
}

/**
* Returns a comma separated string of the possible values that this column
Expand All @@ -90,21 +73,6 @@ default Optional<String> getEnumConstants() {
return getAsString(ENUM_CONSTANTS);
}

/**
* Returns the class that represents the database type.
*
* @return the database type
*/
default Class<?> findDatabaseType() {
final String name = getDatabaseType();

try {
return Class.forName(name);
} catch (final ClassNotFoundException ex) {
throw new SpeedmentConfigException("Could not find database type: '" + name + "'.", ex);
}
}

@Override
default Class<Column> mainInterface() {
return Column.class;
Expand Down
@@ -0,0 +1,67 @@
package com.speedment.runtime.config.trait;

import com.speedment.runtime.config.Document;
import com.speedment.runtime.config.exception.SpeedmentConfigException;
import static com.speedment.runtime.config.trait.HasTypeMapper.DATABASE_TYPE;
import static com.speedment.runtime.config.util.DocumentUtil.newNoSuchElementExceptionFor;
import java.util.Optional;

/**
* Trait for documents that have a {@code TypeMapper} specified.
*
* @author Emil Forslund
* @since 3.0.2
*/
public interface HasTypeMapper extends Document {

/**
* The attribute under which the name of the {@code TypeMapper} is stored in
* the configuration file.
*/
String TYPE_MAPPER = "typeMapper";

/**
* The attribute under which the database type is stored in the
* configuration file. This is used initially to determine the default
* {@code TypeMapper} to use.
*/
String DATABASE_TYPE = "databaseType";

/**
* Returns the name of the mapper class that will be used to generate a java
* representation of the database types.
*
* @return the mapper class
*/
default Optional<String> getTypeMapper() {
return getAsString(TYPE_MAPPER);
}

/**
* Returns the name of the class that represents the database type.
*
* @return the database type class
*/
default String getDatabaseType() {
return getAsString(DATABASE_TYPE)
.orElseThrow(newNoSuchElementExceptionFor(this, DATABASE_TYPE));
}

/**
* Returns the class that represents the database type.
*
* @return the database type
*/
default Class<?> findDatabaseType() {
final String name = getDatabaseType();

try {
return Class.forName(name);
} catch (final ClassNotFoundException ex) {
throw new SpeedmentConfigException(
"Could not find database type: '" + name + "'.",
ex
);
}
}
}

0 comments on commit 38929d7

Please sign in to comment.