Skip to content

Commit

Permalink
Began implementing immutable model
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Jan 7, 2016
1 parent e6e7dba commit eb6aa3b
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 656 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/speedment/config/BaseDocument.java
Expand Up @@ -35,6 +35,11 @@ public Optional<? extends Document> getParent() {
return Optional.ofNullable(parent); return Optional.ofNullable(parent);
} }


@Override
public Map<String, Object> getData() {
return config;
}

@Override @Override
public Optional<Object> get(String key) { public Optional<Object> get(String key) {
return Optional.ofNullable(config.get(key)); return Optional.ofNullable(config.get(key));
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/speedment/config/Document.java
Expand Up @@ -21,15 +21,25 @@ public interface Document {


Optional<? extends Document> getParent(); Optional<? extends Document> getParent();


Map<String, Object> getData();

Optional<Object> get(String key); Optional<Object> get(String key);

OptionalBoolean getAsBoolean(String key); OptionalBoolean getAsBoolean(String key);

OptionalLong getAsLong(String key); OptionalLong getAsLong(String key);

OptionalDouble getAsDouble(String key); OptionalDouble getAsDouble(String key);

OptionalInt getAsInt(String key); OptionalInt getAsInt(String key);

Optional<String> getAsString(String key); Optional<String> getAsString(String key);


void put(String key, Object value); void put(String key, Object value);
MapStream<String, Object> stream();
default MapStream<String, Object> stream() {
return MapStream.of(getData());
}


default <T> Stream<T> children(String key, Function<Map<String, Object>, T> instantiator) { default <T> Stream<T> children(String key, Function<Map<String, Object>, T> instantiator) {
final List<Map<String, Object>> list = final List<Map<String, Object>> list =
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/speedment/config/ImmutableDocument.java
@@ -0,0 +1,77 @@
/*
* Copyright 2016 Speedment, 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.speedment.config;

import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import static java.util.stream.Collectors.toList;
import java.util.stream.Stream;

/**
*
* @author Emil Forslund
*/
public class ImmutableDocument extends BaseDocument {

private final transient Map<String, List<Document>> children;

protected ImmutableDocument(Map<String, Object> data) {
super(Collections.unmodifiableMap(data));
children = new ConcurrentHashMap<>();
}

protected ImmutableDocument(ImmutableDocument parent, Map<String, Object> data) {
super(parent, Collections.unmodifiableMap(data));
children = new ConcurrentHashMap<>();
}

@Override
public final Map<String, Object> getData() {
return super.getData();
}

@Override
public final void put(String key, Object value) {
throwNewUnsupportedOperationExceptionImmutable();
}

@Override
public final <T> Stream<T> children(String key, Function<Map<String, Object>, T> instantiator) {
return children.computeIfAbsent(key, k -> {
final List<Map<String, Object>> list =
(List<Map<String, Object>>) get(k).orElse(null);

if (list == null) {
return new ArrayList<>();
} else {
return list.stream()
.map(Collections::unmodifiableMap)
.map(instantiator)
.map(Document.class::cast)
.collect(toList());
}
}).stream().map(c -> (T) c);
}

public static ImmutableDocument from(Document document) {
return new ImmutableDocument(document.getData());
}
}
@@ -0,0 +1,82 @@
/**
*
* Copyright (c) 2006-2015, 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.internal.core.config.db.immutable;

import com.speedment.config.ImmutableDocument;
import com.speedment.config.db.Column;
import com.speedment.config.db.mapper.TypeMapper;

/**
*
* @author pemi
*/
public final class ImmutableColumn extends ImmutableDocument implements Column {

private final boolean nullable;
private final boolean autoincrement;
private final String alias;
private final String typeMapper;
private final TypeMapper<?, ?> typeMapperObject;
private final String databaseType;
private final Class<?> databaseTypeObject;

public ImmutableColumn(ImmutableTable parent, Column column) {
super(parent, column.getData());
this.nullable = column.isNullable();
this.autoincrement = column.isAutoIncrement();
this.alias = column.getAlias();
this.typeMapper = column.getTypeMapper();
this.typeMapperObject = column.findTypeMapper();
this.databaseType = column.getDatabaseType();
this.databaseTypeObject = column.findDatabaseType();
}

@Override
public String getAlias() {
return alias;
}

@Override
public boolean isNullable() {
return nullable;
}

@Override
public boolean isAutoIncrement() {
return autoincrement;
}

@Override
public String getTypeMapper() {
return typeMapper;
}

@Override
public TypeMapper<?, ?> findTypeMapper() {
return typeMapperObject;
}

@Override
public String getDatabaseType() {
return databaseType;
}

@Override
public Class<?> findDatabaseType() {
return databaseTypeObject;
}
}
Expand Up @@ -14,65 +14,47 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.internal.core.config.*;
import com.speedment.Speedment; import com.speedment.Speedment;
import com.speedment.config.ImmutableDocument;
import com.speedment.config.db.Dbms; import com.speedment.config.db.Dbms;
import com.speedment.config.db.Project; import com.speedment.config.db.Project;
import com.speedment.config.db.Schema; import com.speedment.config.db.Schema;
import com.speedment.config.aspects.Nameable;
import com.speedment.config.aspects.Parent;
import com.speedment.config.db.parameters.DbmsType; import com.speedment.config.db.parameters.DbmsType;
import com.speedment.internal.core.config.aspects.DbmsTypeableHelper;
import groovy.lang.Closure;
import java.util.Optional; import java.util.Optional;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import static java.util.Objects.requireNonNull;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import java.util.OptionalInt;


/** /**
* *
* @author pemi * @author pemi
*/ */
public final class ImmutableDbms extends ImmutableAbstractNamedConfigEntity implements Dbms, DbmsTypeableHelper, ImmutableParentHelper<Schema> { public final class ImmutableDbms extends ImmutableDocument implements Dbms {


private final Speedment speedment; private final String typeName;
private final Optional<Project> parent;
private final ChildHolder<Schema> children;
private final DbmsType type;
private final Optional<String> ipAddress; private final Optional<String> ipAddress;
private final Optional<Integer> port; private final OptionalInt port;
private final Optional<String> username, password; private final Optional<String> username;


public ImmutableDbms(Project parent, Dbms dbms) { public ImmutableDbms(ImmutableProject parent, Dbms dbms) {
super(requireNonNull(dbms).getName(), dbms.isExpanded(), dbms.isEnabled()); super(parent, dbms.getData());
requireNonNull(parent);
// Members
this.speedment = parent.getSpeedment();
this.parent = Optional.of(parent);
this.type = dbms.getType();
this.ipAddress = dbms.getIpAddress();
this.port = dbms.getPort();
this.username = dbms.getUsername();
this.password = dbms.getPassword();
// Children
children = childHolderOf(Schema.class, dbms.stream().map(p -> new ImmutableSchema(this, p)));
}


@Override this.typeName = dbms.getTypeName();
public DbmsType getType() { this.ipAddress = dbms.getIpAddress().orElse(null);
return type; this.port = dbms.getPort().isPresent() ? dbms.getPort().getAsInt() : null;
this.username = dbms.getUsername().orElse(null);
} }


@Override @Override
public void setType(DbmsType dbmsType) { public String getTypeName() {
throwNewUnsupportedOperationExceptionImmutable(); return typeName;
} }


@Override @Override
public Optional<String> getIpAddress() { public Optional<String> getIpAddress() {
return ipAddress; return Optional.ofNullable(ipAddress);
} }


@Override @Override
Expand Down
Expand Up @@ -14,15 +14,15 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.internal.core.config.*; import com.speedment.internal.core.config.*;
import com.speedment.config.db.ForeignKey; import com.speedment.config.db.ForeignKey;
import com.speedment.config.db.ForeignKeyColumn; import com.speedment.config.db.ForeignKeyColumn;
import com.speedment.config.db.Table; import com.speedment.config.db.Table;
import com.speedment.config.aspects.Ordinable; import com.speedment.config.aspects.Ordinable;
import com.speedment.config.aspects.Parent; import com.speedment.config.aspects.Parent;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import groovy.lang.Closure; import groovy.lang.Closure;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.Optional; import java.util.Optional;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.config.db.Column; import com.speedment.config.db.Column;
import com.speedment.config.db.ForeignKey; import com.speedment.config.db.ForeignKey;
Expand All @@ -24,7 +24,7 @@
import com.speedment.config.aspects.Parent; import com.speedment.config.aspects.Parent;
import static com.speedment.internal.core.config.utils.ConfigUtil.thereIsNo; import static com.speedment.internal.core.config.utils.ConfigUtil.thereIsNo;
import com.speedment.internal.core.config.aspects.ColumnableHelper; import com.speedment.internal.core.config.aspects.ColumnableHelper;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.Optional; import java.util.Optional;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
Expand Down
Expand Up @@ -14,15 +14,15 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.internal.core.config.*; import com.speedment.internal.core.config.*;
import com.speedment.config.db.Index; import com.speedment.config.db.Index;
import com.speedment.config.db.IndexColumn; import com.speedment.config.db.IndexColumn;
import com.speedment.config.db.Table; import com.speedment.config.db.Table;
import com.speedment.config.aspects.Ordinable; import com.speedment.config.aspects.Ordinable;
import com.speedment.config.aspects.Parent; import com.speedment.config.aspects.Parent;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import groovy.lang.Closure; import groovy.lang.Closure;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.Optional; import java.util.Optional;
Expand Down
Expand Up @@ -14,15 +14,15 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.config.db.Column; import com.speedment.config.db.Column;
import com.speedment.config.db.Index; import com.speedment.config.db.Index;
import com.speedment.config.db.IndexColumn; import com.speedment.config.db.IndexColumn;
import com.speedment.config.aspects.Parent; import com.speedment.config.aspects.Parent;
import com.speedment.internal.core.config.aspects.ColumnableHelper; import com.speedment.internal.core.config.aspects.ColumnableHelper;
import com.speedment.config.db.parameters.OrderType; import com.speedment.config.db.parameters.OrderType;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.Optional; import java.util.Optional;


Expand Down
Expand Up @@ -14,15 +14,15 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.internal.core.config.aspects.*; import com.speedment.internal.core.config.aspects.*;
import com.speedment.config.aspects.Child; import com.speedment.config.aspects.Child;
import com.speedment.internal.core.config.ChildHolder; import com.speedment.internal.core.config.ChildHolder;
import java.util.Optional; import java.util.Optional;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;


/** /**
* *
Expand Down
Expand Up @@ -14,7 +14,7 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.speedment.internal.core.config.immutable; package com.speedment.internal.core.config.db.immutable;


import com.speedment.internal.core.config.*; import com.speedment.internal.core.config.*;
import com.speedment.Speedment; import com.speedment.Speedment;
Expand All @@ -25,7 +25,7 @@
import com.speedment.config.aspects.Parent; import com.speedment.config.aspects.Parent;
import com.speedment.exception.SpeedmentException; import com.speedment.exception.SpeedmentException;
import java.util.Optional; import java.util.Optional;
import static com.speedment.internal.core.config.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable; import static com.speedment.internal.core.config.db.immutable.ImmutableUtil.throwNewUnsupportedOperationExceptionImmutable;
import groovy.lang.Closure; import groovy.lang.Closure;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.stream.Stream; import java.util.stream.Stream;
Expand Down

0 comments on commit eb6aa3b

Please sign in to comment.