Skip to content

Commit

Permalink
Runtime: Move TypeMapperComponent to Generator module
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Jul 21, 2016
1 parent a443eaf commit 7c369ee
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 157 deletions.
Expand Up @@ -65,9 +65,9 @@
import static com.speedment.generator.StandardTranslatorKey.*;
import com.speedment.generator.internal.typetoken.TypeTokenGeneratorImpl;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNull;

@IncludeInjectable({
TypeMapperComponentImpl.class,
TranslatorManagerImpl.class,
JavaLanguageNamerImpl.class,
JavaGenerator.class,
Expand Down
@@ -1,149 +1,150 @@
/**
*
* Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
*
* 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.speedment.runtime.internal.component;

import com.speedment.common.injector.Injector;
import com.speedment.common.injector.annotation.Inject;
import com.speedment.runtime.component.TypeMapperComponent;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.mapper.TypeMapper;
import com.speedment.runtime.config.mapper.bigdecimal.BigDecimalToDouble;
import com.speedment.runtime.config.mapper.integer.IntegerZeroOneToBooleanMapper;
import com.speedment.runtime.config.mapper.largeobject.ClobToStringMapper;
import com.speedment.runtime.config.mapper.string.StringToLocaleMapper;
import com.speedment.runtime.config.mapper.string.TrueFalseStringToBooleanMapper;
import com.speedment.runtime.config.mapper.string.YesNoStringToBooleanMapper;
import com.speedment.runtime.config.mapper.time.DateToIntMapper;
import com.speedment.runtime.config.mapper.time.DateToLongMapper;
import com.speedment.runtime.config.mapper.time.TimeToIntMapper;
import com.speedment.runtime.config.mapper.time.TimeToLongMapper;
import com.speedment.runtime.config.mapper.time.TimestampToIntMapper;
import com.speedment.runtime.config.mapper.time.TimestampToLongMapper;
import com.speedment.runtime.exception.SpeedmentException;
import com.speedment.runtime.license.Software;
import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
*
* @author Emil Forslund
* @since 2.2.0
*/
public final class TypeMapperComponentImpl extends InternalOpenSourceComponent implements TypeMapperComponent {

private final Map<String, List<Supplier<TypeMapper<?, ?>>>> mappers;
private @Inject Injector injector;

/**
* Constructs the component.
*/
public TypeMapperComponentImpl() {
this.mappers = new ConcurrentHashMap<>();

// Special time mappers
install(Date.class, DateToLongMapper::new);
install(Timestamp.class, TimestampToLongMapper::new);
install(Time.class, TimeToLongMapper::new);
install(Date.class, DateToIntMapper::new);
install(Timestamp.class, TimestampToIntMapper::new);
install(Time.class, TimeToIntMapper::new);

// Special string mappers
install(String.class, StringToLocaleMapper::new);
install(String.class, TrueFalseStringToBooleanMapper::new);
install(String.class, YesNoStringToBooleanMapper::new);

// Special BigDecimal object mappers
install(BigDecimal.class, BigDecimalToDouble::new);

// Special Large object mappers
install(Clob.class, ClobToStringMapper::new);

// Special integer mappers
install(Integer.class, IntegerZeroOneToBooleanMapper::new);
}

@Override
protected String getDescription() {
return "Holds all the type mappers that have been installed into the Speedment Platform. " +
"A Type Mapper is used to convert between database types and java types.";
}

@Override
public void install(Class<?> databaseType, Supplier<TypeMapper<?, ?>> typeMapperConstructor) {
mappers.computeIfAbsent(
databaseType.getName(),
n -> new LinkedList<>()
).add(typeMapperConstructor);
}

@Override
public final Stream<TypeMapper<?, ?>> mapFrom(Class<?> databaseType) {
return mappers.getOrDefault(databaseType.getName(), Collections.emptyList())
.stream()
.map(Supplier::get)
.map(injector::inject);
}

@Override
public Stream<TypeMapper<?, ?>> stream() {
return mappers.values().stream()
.flatMap(List::stream)
.map(Supplier::get)
.map(injector::inject);
}

@Override
public Optional<TypeMapper<?, ?>> get(String absoluteClassName) {
return stream()
.filter(tm -> tm.getClass().getName().equals(absoluteClassName))
.findAny();
}

@Override
public TypeMapper<?, ?> get(Column column) {
return injector.inject(
column.getTypeMapper().map(name -> {
try {
@SuppressWarnings("unchecked")
final TypeMapper<Object, Object> mapper =
(TypeMapper<Object, Object>) Class.forName(name).newInstance();
return mapper;
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
throw new SpeedmentException("Could not instantiate TypeMapper: '" + name + "'.", ex);
}
}).orElseGet(TypeMapper::identity)
);
}

@Override
public Stream<Software> getDependencies() {
return Stream.empty();
}
/**
*
* Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
*
* 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.speedment.generator.internal.component;

import com.speedment.common.injector.Injector;
import com.speedment.common.injector.annotation.Inject;
import com.speedment.runtime.component.TypeMapperComponent;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.mapper.TypeMapper;
import com.speedment.runtime.config.mapper.bigdecimal.BigDecimalToDouble;
import com.speedment.runtime.config.mapper.integer.IntegerZeroOneToBooleanMapper;
import com.speedment.runtime.config.mapper.largeobject.ClobToStringMapper;
import com.speedment.runtime.config.mapper.string.StringToLocaleMapper;
import com.speedment.runtime.config.mapper.string.TrueFalseStringToBooleanMapper;
import com.speedment.runtime.config.mapper.string.YesNoStringToBooleanMapper;
import com.speedment.runtime.config.mapper.time.DateToIntMapper;
import com.speedment.runtime.config.mapper.time.DateToLongMapper;
import com.speedment.runtime.config.mapper.time.TimeToIntMapper;
import com.speedment.runtime.config.mapper.time.TimeToLongMapper;
import com.speedment.runtime.config.mapper.time.TimestampToIntMapper;
import com.speedment.runtime.config.mapper.time.TimestampToLongMapper;
import com.speedment.runtime.exception.SpeedmentException;
import com.speedment.runtime.internal.component.InternalOpenSourceComponent;
import com.speedment.runtime.license.Software;
import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
*
* @author Emil Forslund
* @since 2.2.0
*/
public final class TypeMapperComponentImpl extends InternalOpenSourceComponent implements TypeMapperComponent {

private final Map<String, List<Supplier<TypeMapper<?, ?>>>> mappers;
private @Inject Injector injector;

/**
* Constructs the component.
*/
public TypeMapperComponentImpl() {
this.mappers = new ConcurrentHashMap<>();

// Special time mappers
install(Date.class, DateToLongMapper::new);
install(Timestamp.class, TimestampToLongMapper::new);
install(Time.class, TimeToLongMapper::new);
install(Date.class, DateToIntMapper::new);
install(Timestamp.class, TimestampToIntMapper::new);
install(Time.class, TimeToIntMapper::new);

// Special string mappers
install(String.class, StringToLocaleMapper::new);
install(String.class, TrueFalseStringToBooleanMapper::new);
install(String.class, YesNoStringToBooleanMapper::new);

// Special BigDecimal object mappers
install(BigDecimal.class, BigDecimalToDouble::new);

// Special Large object mappers
install(Clob.class, ClobToStringMapper::new);

// Special integer mappers
install(Integer.class, IntegerZeroOneToBooleanMapper::new);
}

@Override
protected String getDescription() {
return "Holds all the type mappers that have been installed into the Speedment Platform. " +
"A Type Mapper is used to convert between database types and java types.";
}

@Override
public void install(Class<?> databaseType, Supplier<TypeMapper<?, ?>> typeMapperConstructor) {
mappers.computeIfAbsent(
databaseType.getName(),
n -> new LinkedList<>()
).add(typeMapperConstructor);
}

@Override
public final Stream<TypeMapper<?, ?>> mapFrom(Class<?> databaseType) {
return mappers.getOrDefault(databaseType.getName(), Collections.emptyList())
.stream()
.map(Supplier::get)
.map(injector::inject);
}

@Override
public Stream<TypeMapper<?, ?>> stream() {
return mappers.values().stream()
.flatMap(List::stream)
.map(Supplier::get)
.map(injector::inject);
}

@Override
public Optional<TypeMapper<?, ?>> get(String absoluteClassName) {
return stream()
.filter(tm -> tm.getClass().getName().equals(absoluteClassName))
.findAny();
}

@Override
public TypeMapper<?, ?> get(Column column) {
return injector.inject(
column.getTypeMapper().map(name -> {
try {
@SuppressWarnings("unchecked")
final TypeMapper<Object, Object> mapper =
(TypeMapper<Object, Object>) Class.forName(name).newInstance();
return mapper;
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
throw new SpeedmentException("Could not instantiate TypeMapper: '" + name + "'.", ex);
}
}).orElseGet(TypeMapper::identity)
);
}

@Override
public Stream<Software> getDependencies() {
return Stream.empty();
}
}
Expand Up @@ -17,7 +17,6 @@
package com.speedment.runtime.config.mapper;

import com.speedment.runtime.annotation.Api;
import com.speedment.runtime.component.TypeMapperComponent;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.typetoken.TypeToken;
import com.speedment.runtime.internal.config.mapper.IdentityTypeMapper;
Expand All @@ -29,7 +28,7 @@
/**
* A type mapper contains logic for converting between the database and the java
* type for a field. Implementations of this class should be installed in the
* {@link TypeMapperComponent}.
* {@code TypeMapperComponent}.
*
* @param <DB_TYPE> the type as it is represented in the JDBC driver
* @param <JAVA_TYPE> the type as it should be represented in generated code
Expand Down
Expand Up @@ -22,7 +22,6 @@
import com.speedment.common.logger.LoggerManager;
import com.speedment.runtime.component.DbmsHandlerComponent;
import com.speedment.runtime.component.ProjectComponent;
import com.speedment.runtime.component.TypeMapperComponent;
import com.speedment.runtime.component.connectionpool.ConnectionPoolComponent;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.Dbms;
Expand All @@ -35,7 +34,6 @@
import com.speedment.runtime.config.Project;
import com.speedment.runtime.config.Schema;
import com.speedment.runtime.config.Table;
import com.speedment.runtime.config.mapper.TypeMapper;
import com.speedment.runtime.config.mutator.ForeignKeyColumnMutator;
import com.speedment.runtime.config.parameter.DbmsType;
import com.speedment.runtime.config.parameter.OrderType;
Expand Down Expand Up @@ -63,7 +61,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -92,7 +89,6 @@ public abstract class AbstractDbmsMetadataHandler implements DbmsMetadataHandler

private @Inject ConnectionPoolComponent connectionPoolComponent;
private @Inject DbmsHandlerComponent dbmsHandlerComponent;
private @Inject TypeMapperComponent typeMapperComponent;
private @Inject ProjectComponent projectComponent;
private JavaTypeMap javaTypeMap;

Expand Down
Expand Up @@ -35,7 +35,6 @@
import com.speedment.runtime.internal.component.PrimaryKeyFactoryComponentImpl;
import com.speedment.runtime.internal.component.ProjectComponentImpl;
import com.speedment.runtime.internal.component.ResultSetMapperComponentImpl;
import com.speedment.runtime.internal.component.TypeMapperComponentImpl;
import com.speedment.runtime.internal.config.dbms.StandardDbmsTypes;
import com.speedment.runtime.manager.Manager;

Expand All @@ -59,7 +58,6 @@
PrimaryKeyFactoryComponentImpl.class,
ProjectComponentImpl.class,
ResultSetMapperComponentImpl.class,
TypeMapperComponentImpl.class,
StandardDbmsTypes.class
})
public abstract class AbstractSpeedment implements Speedment {
Expand Down

0 comments on commit 7c369ee

Please sign in to comment.