Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default values evolution #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/org/globsframework/metamodel/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ default LongField asLongField() {
return (LongField) this;
}

default BigDecimalField asBigDecimalField() {
if (!(this instanceof BigDecimalField)) {
throw new RuntimeException(getFullName() + " is not a BigDecimalField but a " + getDataType());
}
return (BigDecimalField) this;
}

default DateField asDateField() {
if (!(this instanceof DateField)) {
throw new RuntimeException(getFullName() + " is not a DateField but a " + getDataType());
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/globsframework/metamodel/GlobType.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.globsframework.metamodel;

import org.globsframework.metamodel.fields.FieldVisitor;
import org.globsframework.metamodel.fields.FieldVisitorWithContext;
import org.globsframework.metamodel.impl.DefaultValuesFieldVisitor;
import org.globsframework.metamodel.index.Index;
import org.globsframework.metamodel.properties.PropertyHolder;
import org.globsframework.model.GlobFactory;
import org.globsframework.model.Key;
import org.globsframework.model.MutableGlob;
import org.globsframework.utils.exceptions.ItemNotFound;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Optional;
Expand Down Expand Up @@ -57,6 +60,26 @@ default MutableGlob instantiate() {
return getGlobFactory().create();
}

default MutableGlob instantiateWithDefaults() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Michele,
Sauf si quelque chose m'a échapper, le visiteur est un double dispatcheur, qui remplace aussi le switch.

MutableGlob glob = getGlobFactory().create();
FieldVisitorWithContext<MutableGlob> visitor = new DefaultValuesFieldVisitor();
Arrays.stream(this.getFields()).forEach(field -> {
try {
switch (field.getDataType()) {
case Boolean -> visitor.visitBoolean(field.asBooleanField(), glob);
case Double -> visitor.visitDouble(field.asDoubleField(), glob);
case Integer -> visitor.visitInteger(field.asIntegerField(), glob);
case Long -> visitor.visitLong(field.asLongField(), glob);
case String -> visitor.visitString(field.asStringField(), glob);
case BigDecimal -> visitor.visitBigDecimal(field.asBigDecimalField(), glob);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return glob;
}

default <T extends FieldVisitor> T accept(T visitor) throws Exception {
return getGlobFactory().accept(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.globsframework.metamodel.annotations;

import org.globsframework.metamodel.GlobType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({ElementType.FIELD})
public @interface DefaultBigDecimal {
String value();

GlobType TYPE = DefaultBigDecimalAnnotationType.DESC;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.globsframework.metamodel.annotations;

import org.globsframework.metamodel.GlobType;
import org.globsframework.metamodel.GlobTypeLoader;
import org.globsframework.metamodel.GlobTypeLoaderFactory;
import org.globsframework.metamodel.fields.BigDecimalField;
import org.globsframework.metamodel.fields.DoubleField;
import org.globsframework.model.Glob;
import org.globsframework.model.Key;

import java.math.BigDecimal;

public class DefaultBigDecimalAnnotationType {

public static GlobType DESC;

@DefaultFieldValue
public static BigDecimalField DEFAULT_VALUE;

@InitUniqueKey
public static Key UNIQUE_KEY;

public static Glob create(String defaultBigDecimal) {
return DESC.instantiate().set(DEFAULT_VALUE, new BigDecimal(defaultBigDecimal));
}

static {
GlobTypeLoader loader = GlobTypeLoaderFactory.create(DefaultBigDecimalAnnotationType.class);
loader.register(GlobCreateFromAnnotation.class, annotation -> create(((DefaultBigDecimal)annotation).value()))
.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.math.BigDecimal;

public class DefaultBigDecimalField extends AbstractField implements BigDecimalField {
public DefaultBigDecimalField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Long defaultValue) {
public DefaultBigDecimalField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, BigDecimal defaultValue) {
super(name, globType, BigDecimal.class, index, keyIndex, isKeyField, defaultValue, DataType.BigDecimal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.globsframework.model.Glob;
import org.globsframework.model.Key;

import java.math.BigDecimal;
import java.util.List;

public class DefaultFieldFactory {
Expand Down Expand Up @@ -92,8 +93,9 @@ public DefaultBooleanField addBoolean(String name,

public DefaultBigDecimalField addBigDecimal(String name,
boolean isKeyField,
int keyIndex, int index) {
return add(new DefaultBigDecimalField(name, type, index, isKeyField, keyIndex, null), false);
int keyIndex, int index,
BigDecimal defaultValue) {
return add(new DefaultBigDecimalField(name, type, index, isKeyField, keyIndex, defaultValue), false);
}

public DefaultBigDecimalArrayField addBigDecimalArray(String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.globsframework.metamodel.utils.MutableAnnotations;
import org.globsframework.model.Glob;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -172,7 +173,8 @@ private DefaultDoubleArrayField createDoubleArrayField(String fieldName, Collect
private DefaultBigDecimalField createBigDecimalField(String fieldName, Collection<Glob> globAnnotations) {
MutableAnnotations annotations = adaptAnnotation(globAnnotations);
Glob key = annotations.findAnnotation(KeyAnnotationType.UNIQUE_KEY);
DefaultBigDecimalField bigDecimalField = factory.addBigDecimal(fieldName, key != null, getKeyId(key), index);
BigDecimal defaultValue = annotations.getValueOrDefault(DefaultBigDecimalAnnotationType.UNIQUE_KEY, DefaultBigDecimalAnnotationType.DEFAULT_VALUE, null);
DefaultBigDecimalField bigDecimalField = factory.addBigDecimal(fieldName, key != null, getKeyId(key), index, defaultValue);
bigDecimalField.addAnnotations(annotations.streamAnnotations());
index++;
return bigDecimalField;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.globsframework.metamodel.impl;

import org.globsframework.metamodel.annotations.*;
import org.globsframework.metamodel.fields.*;
import org.globsframework.model.MutableGlob;

import java.math.BigDecimal;

public class DefaultValuesFieldVisitor implements FieldVisitorWithContext<MutableGlob> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il y a un Abstract dans l'interface pour ne pas avoir a implementer les methodes vide

@Override
public void visitInteger(IntegerField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultIntegerAnnotationType.UNIQUE_KEY)) {
context.set(field.asIntegerField(), (int) field.getDefaultValue());
}
}

@Override
public void visitIntegerArray(IntegerArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitDouble(DoubleField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultDoubleAnnotationType.UNIQUE_KEY)) {
context.set(field.asDoubleField(), (double) field.getDefaultValue());
}
}

@Override
public void visitDoubleArray(DoubleArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitString(StringField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultStringAnnotationType.UNIQUE_KEY)) {
context.set(field.asStringField(), (String) field.getDefaultValue());
}
}

@Override
public void visitStringArray(StringArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitBoolean(BooleanField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultBooleanAnnotationType.UNIQUE_KEY)) {
context.set(field.asBooleanField(), (boolean) field.getDefaultValue());
}
}

@Override
public void visitBooleanArray(BooleanArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitBigDecimal(BigDecimalField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultBigDecimalAnnotationType.UNIQUE_KEY)) {
context.set(field.asBigDecimalField(), (BigDecimal) field.getDefaultValue());
}

}

@Override
public void visitBigDecimalArray(BigDecimalArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitLong(LongField field, MutableGlob context) throws Exception {
if (field.hasAnnotation(DefaultLongAnnotationType.UNIQUE_KEY)) {
context.set(field.asLongField(), (long) field.getDefaultValue());
}
}

@Override
public void visitLongArray(LongArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitDate(DateField field, MutableGlob context) throws Exception {
}

@Override
public void visitDateTime(DateTimeField field, MutableGlob context) throws Exception {

}

@Override
public void visitBlob(BlobField field, MutableGlob context) throws Exception {

}

@Override
public void visitGlob(GlobField field, MutableGlob context) throws Exception {

}

@Override
public void visitGlobArray(GlobArrayField field, MutableGlob context) throws Exception {

}

@Override
public void visitUnionGlob(GlobUnionField field, MutableGlob context) throws Exception {

}

@Override
public void visitUnionGlobArray(GlobArrayUnionField field, MutableGlob context) throws Exception {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -309,7 +310,10 @@ else if (DateTimeField.class.isAssignableFrom(fieldClass)) {
return fieldFactory.addDateTime(name, isKeyField, keyIndex, index);
}
else if (BigDecimalField.class.isAssignableFrom(fieldClass)) {
return fieldFactory.addBigDecimal(name, isKeyField, keyIndex, index);
DefaultBigDecimal defaultBigDecimal = field.getAnnotation(DefaultBigDecimal.class);
return fieldFactory.addBigDecimal(name, isKeyField, keyIndex, index,
defaultBigDecimal != null ? new BigDecimal(defaultBigDecimal.value()) : null
);
}
else if (BigDecimalArrayField.class.isAssignableFrom(fieldClass)) {
return fieldFactory.addBigDecimalArray(name, isKeyField, keyIndex, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class DummyObjectWithDefaultValues {
@DefaultInteger(7)
public static IntegerField INTEGER;

@DefaultBigDecimal("1.61803398875")
public static BigDecimalField BIG_DECIMAL;

@DefaultLong(5l)
public static LongField LONG;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.globsframework.metamodel;

import org.globsframework.model.MutableGlob;
import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.*;

public class GlobTypeInstantiateWithDefaultsTest {
@Test
public void test() {
MutableGlob globWithDefaults = DummyObjectWithDefaultValues.TYPE.instantiateWithDefaults();

assertEquals(globWithDefaults.get(DummyObjectWithDefaultValues.STRING), "Hello");
assertEquals(globWithDefaults.get(DummyObjectWithDefaultValues.DOUBLE), Double.valueOf(3.14159265));
assertEquals(globWithDefaults.get(DummyObjectWithDefaultValues.INTEGER), Integer.valueOf(7));
assertEquals(globWithDefaults.get(DummyObjectWithDefaultValues.LONG), Long.valueOf(5));
assertEquals(globWithDefaults.get(DummyObjectWithDefaultValues.BIG_DECIMAL), new BigDecimal("1.61803398875"));
assertNull(globWithDefaults.get(DummyObjectWithDefaultValues.ID));
assertNull(globWithDefaults.get(DummyObjectWithDefaultValues.LINK));
assertTrue(globWithDefaults.get(DummyObjectWithDefaultValues.BOOLEAN));

MutableGlob glob = DummyObjectWithDefaultValues.TYPE.instantiate();
assertNull(glob.get(DummyObjectWithDefaultValues.STRING));
assertNull(glob.get(DummyObjectWithDefaultValues.DOUBLE));
assertNull(glob.get(DummyObjectWithDefaultValues.INTEGER));
assertNull(glob.get(DummyObjectWithDefaultValues.LONG));
assertNull(glob.get(DummyObjectWithDefaultValues.BIG_DECIMAL));
assertNull(glob.get(DummyObjectWithDefaultValues.ID));
assertNull(glob.get(DummyObjectWithDefaultValues.LINK));
assertNull(glob.get(DummyObjectWithDefaultValues.BOOLEAN));

}
}