Skip to content

Commit

Permalink
Add GlobTypeBuilder.declareFrom to not duplicate annotations => WARN …
Browse files Browse the repository at this point in the history
…: FieldName annotation is not replaced.
  • Loading branch information
MarcGuiot committed Oct 1, 2024
1 parent 772dd6e commit b453099
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public interface GlobTypeBuilder {

Field declare(String fieldName, DataType dataType, Collection<Glob> annotations);

Field declareFrom(String fieldName, Field field);

default GlobTypeBuilder addStringField(String fieldName, Glob... annotations) {
return addStringField(fieldName, Arrays.asList(annotations));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;

import java.util.LinkedHashMap;

abstract public class AbstractField extends DefaultAnnotations {
private final int index;
private final int keyIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultGlobUnionArrayField extends AbstractField implements GlobArrayUnionField {
private final Map<String, GlobType> targetTypes;
private volatile Map<String, GlobType> targetTypes;

public DefaultGlobUnionArrayField(String name, GlobType globType, Collection<GlobType> targetTypes,
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob[].class, index, keyIndex, isKeyField, null, DataType.GlobUnionArray, annotations);
this.targetTypes = new ConcurrentHashMap<>();
targetTypes.forEach(this::__add__);
this.targetTypes = new HashMap<>();
targetTypes.forEach(type -> this.targetTypes.put(type.getName(), type));
}

public Collection<GlobType> getTargetTypes() {
return targetTypes.values();
}

public void __add__(GlobType t) {
this.targetTypes.put(t.getName(), t);
synchronized public void __add__(GlobType t) {
Map<String, GlobType> tmp = new HashMap<>(targetTypes);
tmp.put(t.getName(), t);
targetTypes = tmp;
}

public GlobType getTargetType(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultGlobUnionField extends AbstractField implements GlobUnionField {
private final Map<String, GlobType> targetTypes;
private volatile Map<String, GlobType> targetTypes;

public DefaultGlobUnionField(String name, GlobType globType, Collection<GlobType> targetTypes,
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob.class, index, keyIndex, isKeyField, null, DataType.GlobUnion, annotations);
this.targetTypes = new ConcurrentHashMap<>();
targetTypes.forEach(this::__add__);
this.targetTypes = new HashMap<>();
targetTypes.forEach(type -> this.targetTypes.put(type.getName(), type));
}

public Collection<GlobType> getTargetTypes() {
return targetTypes.values();
}

public void __add__(GlobType t) {
this.targetTypes.put(t.getName(), t);
synchronized public void __add__(GlobType t) {
Map<String, GlobType> tmp = new HashMap<>(targetTypes);
tmp.put(t.getName(), t);
targetTypes = tmp;
}

public GlobType getTargetType(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public DefaultAnnotations(Collection<Glob> annotations) {
}
}

// annotations.size() + 1 can be false if the glob key is already present
// but annotations is expected to not be changed

public MutableAnnotations addAnnotation(Glob glob) {
if (glob != null) {
synchronized (this) {
Expand Down Expand Up @@ -103,4 +106,8 @@ public Glob getAnnotation(Key key) {
public Glob findAnnotation(Key key) {
return annotations.get(key);
}

HashContainer<Key, Glob> getInternal() {
return annotations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.container.specific.HashEmptyGlobContainer;

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

Expand Down Expand Up @@ -454,6 +455,61 @@ public Field declare(String fieldName, DataType dataType, Collection<Glob> annot
throw new RuntimeException("creation of " + dataType + " not possible without additional parameter (globType)");
}

/*
TODO : remove annotations FieldName, Index, DefaultValue from annotations : it is a duplication.
Or replace them here with the
*/

public Field declareFrom(String name, Field field) {
boolean isKeyField = field.isKeyField();
final HashContainer<Key, Glob> hashContainer = ((DefaultAnnotations) field).getInternal();
// HashContainer<Key, Glob> duplicate = hashContainer.duplicate();
// MutableGlob glob = FieldName.create(name);
// duplicate.put(glob.getKey(), glob);

Field newField = switch (field.getDataType()) {
case String -> factory.addString(name, field.isKeyField(), keyIndex, index,
((String) field.getDefaultValue()), hashContainer);
case StringArray -> factory.addStringArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case Double ->
factory.addDouble(name, field.isKeyField(), keyIndex, index, ((Double) field.getDefaultValue()),
hashContainer);
case DoubleArray -> factory.addDoubleArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case BigDecimal ->
factory.addBigDecimal(name, field.isKeyField(), keyIndex, index, ((BigDecimal) field.getDefaultValue()),
hashContainer);
case BigDecimalArray ->
factory.addBigDecimalArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case Long -> factory.addLong(name, field.isKeyField(), keyIndex, index, ((Long) field.getDefaultValue()),
hashContainer);
case LongArray -> factory.addLongArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case Integer ->
factory.addInteger(name, field.isKeyField(), keyIndex, index, ((Integer) field.getDefaultValue()),
hashContainer);
case IntegerArray -> factory.addIntegerArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case Boolean ->
factory.addBoolean(name, field.isKeyField(), keyIndex, index, ((Boolean) field.getDefaultValue()),
hashContainer);
case BooleanArray -> factory.addBooleanArray(name, field.isKeyField(), keyIndex, index, hashContainer);
case Date -> factory.addDate(name, field.isKeyField(), keyIndex, index, hashContainer);
case DateTime -> factory.addDateTime(name, field.isKeyField(), keyIndex, index, hashContainer);
case Bytes -> factory.addBlob(name, index, hashContainer);
case Glob ->
factory.addGlob(name, ((GlobField) field).getTargetType(), isKeyField, keyIndex, index, hashContainer);
case GlobArray ->
factory.addGlobArray(name, ((GlobArrayField) field).getTargetType(), isKeyField, keyIndex, index, hashContainer);
case GlobUnion ->
factory.addGlobUnion(name, ((GlobUnionField) field).getTargetTypes(), index, hashContainer);
case GlobUnionArray ->
factory.addGlobArrayUnion(name, ((GlobUnionField) field).getTargetTypes(), index, hashContainer);
};
index++;
if (isKeyField) {
keyIndex++;
}
return newField;
}

public <T> void register(Class<T> klass, T t) {
type.register(klass, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public Hash4ElementContainer(HashTwoElementContainer<T, D> elementContainer, T k
this.key4 = (T) HashOneElementContainer.NULL;
}

public Hash4ElementContainer(Hash4ElementContainer<T, D> tdHash4ElementContainer) {
key1 = tdHash4ElementContainer.key1;
value1 = tdHash4ElementContainer.value1;
key2 = tdHash4ElementContainer.key2;
value2 = tdHash4ElementContainer.value2;
key3 = tdHash4ElementContainer.key3;
value3 = tdHash4ElementContainer.value3;
key4 = tdHash4ElementContainer.key4;
value4 = tdHash4ElementContainer.value4;
}

public D get(T key) {
return Utils.equalWithHash(key, this.key1) ? value1 : Utils.equalWithHash(key, this.key2) ? value2 : Utils.equalWithHash(key, this.key3) ? value3 : Utils.equalWithHash(key, this.key4) ? value4 : null;
}
Expand Down Expand Up @@ -120,6 +131,10 @@ public void remove() {
};
}

public HashContainer<T, D> duplicate() {
return new Hash4ElementContainer<>(this);
}

public TwoElementIterator<T, D> entryIterator() {
return new TwoElementIterator<T, D>() {
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static <T, D> HashContainer<T, D> empty() {

Iterator<D> values();

HashContainer<T, D> duplicate();

TwoElementIterator<T, D> entryIterator();

D remove(T value);
Expand All @@ -31,7 +33,7 @@ static <T, D> HashContainer<T, D> empty() {

boolean containsKey(T key);

default Stream<D> stream(){
default Stream<D> stream() {
return StreamSupport.stream(Spliterators.spliterator(values(), size(), Spliterator.NONNULL), false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public Iterator<D> values() {
return Collections.emptyIterator();
}

public HashContainer<T, D> duplicate() {
return this;
}

public TwoElementIterator<T, D> entryIterator() {
return EmptyTwoElementIterator.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public HashMapContainer(T key, D value) {
elements.put(key, value);
}

public HashMapContainer(HashMapContainer<T, D> tdHashMapContainer) {
elements = new HashMap<>(tdHashMapContainer.elements);
}

public D get(T key) {
return elements.get(key);
}
Expand All @@ -33,6 +37,10 @@ public Iterator<D> values() {
return elements.values().iterator();
}

public HashContainer<T, D> duplicate() {
return new HashMapContainer<>(this);
}

public TwoElementIterator<T, D> entryIterator() {
return new TwoElementIterator<T, D>() {
Iterator<Map.Entry<T, D>> iterator = elements.entrySet().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public Iterator<D> values() {
return new OneStepIterator();
}

public HashContainer<T, D> duplicate() {
return new HashOneElementContainer<>(this.key, this.value);
}

public TwoElementIterator<T, D> entryIterator() {

return new TwoElementIterator<T, D>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public HashTwoElementContainer(HashOneElementContainer<T, D> elementContainer, T
this.value2 = value;
}

public HashTwoElementContainer(HashTwoElementContainer<T, D> tdHashTwoElementContainer) {
this.key1 = tdHashTwoElementContainer.getKey1();
this.value1 = tdHashTwoElementContainer.getValue1();
this.key2 = tdHashTwoElementContainer.getKey2();
this.value2 = tdHashTwoElementContainer.getValue2();
}

public D get(T key) {
return Utils.equalWithHash(key, this.key1) ? value1 : Utils.equalWithHash(key, this.key2) ? value2 : null;
}
Expand Down Expand Up @@ -58,6 +65,10 @@ public Iterator<D> values() {
return new TwoStepIterator();
}

public HashContainer<T, D> duplicate() {
return new HashTwoElementContainer<>(this);
}

public TwoElementIterator<T, D> entryIterator() {
return new TwoElementIterator<T, D>() {
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public class Hash4GlobKeyContainer implements HashContainer<Key, Glob> {
public Hash4GlobKeyContainer() {
}

public Hash4GlobKeyContainer(Glob value1, Glob value2, Glob value3, Glob value4) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}

public HashContainer<Key, Glob> duplicate() {
return new Hash4GlobKeyContainer(value1, value2, value3, value4);
}

public Hash4GlobKeyContainer(HashTwoGlobKeyContainer elementContainer, Glob value) {
value1 = elementContainer.getValue1();
value2 = elementContainer.getValue2();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Iterator<Glob> values() {
return Collections.emptyIterator();
}

public HashContainer<Key, Glob> duplicate() {
return this;
}

public TwoElementIterator<Key, Glob> entryIterator() {
return EmptyTwoElementIterator.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.globsframework.core.utils.Utils;
import org.globsframework.core.utils.container.hash.HashContainer;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

Expand All @@ -28,6 +29,11 @@ public HashMapGlobKeyContainer(int size) {
this.values = new Values(roundUpToPowerOf2(size));
}

public HashMapGlobKeyContainer(HashMapGlobKeyContainer hashMapGlobKeyContainer) {
this.values = hashMapGlobKeyContainer.values.duplicate();
size = hashMapGlobKeyContainer.size;
}

private static int roundUpToPowerOf2(int number) {
int rounded;
return (rounded = Integer.highestOneBit(number)) != 0
Expand Down Expand Up @@ -78,6 +84,10 @@ public Iterator<Glob> values() {
return iterate(values);
}

public HashContainer<Key, Glob> duplicate() {
return new HashMapGlobKeyContainer(this);
}

public TwoElementIterator<Key, Glob> entryIterator() {
final Iterator<Glob> values = values();
return new KeyGlobTwoElementIterator(values);
Expand Down Expand Up @@ -120,6 +130,19 @@ public Values(int size) {
threshold = (int) (size * LOAD_FACTOR);
}

public Values(Values org) {
this.values = Arrays.copyOf(org.values, org.values.length);
if (org.next != null) {
this.next = org.next.duplicate();
}
count = org.count;
threshold = org.threshold;
}

Values duplicate() {
return new Values(this);
}

public Glob getValue(int hash, Key key) {
Glob glob = values[indexFor(hash, values.length)];
if (glob == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class HashOneGlobKeyContainer implements HashContainer<Key, Glob> {
public HashOneGlobKeyContainer() {
}

public HashOneGlobKeyContainer(Glob value) {
this.value = value;
}

public HashOneGlobKeyContainer(Key key, Glob value) {
checkEqual(key, value);
this.value = value;
Expand Down Expand Up @@ -51,6 +55,10 @@ public Iterator<Glob> values() {
return new OneStepIterator();
}

public HashContainer<Key, Glob> duplicate() {
return new HashOneGlobKeyContainer(value);
}

public TwoElementIterator<Key, Glob> entryIterator() {
return new TwoElementIterator<Key, Glob>() {
boolean isNext = value != null;
Expand Down
Loading

0 comments on commit b453099

Please sign in to comment.