Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jul 3, 2024
1 parent fb4e472 commit 76692ce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
8 changes: 2 additions & 6 deletions semtypes/src/main/java/io/ballerina/types/PredefinedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,8 @@ BT_CELL, bddAtom(ATOM_CELL_OBJECT_MEMBER_VISIBILITY)
private static final int BDD_REC_ATOM_OBJECT_READONLY = 1;

private static final RecAtom OBJECT_RO_REC_ATOM = RecAtom.createRecAtom(BDD_REC_ATOM_OBJECT_READONLY);
static {
OBJECT_RO_REC_ATOM.setKind(Atom.Kind.MAPPING_ATOM);
}
public static final BddNode MAPPING_SUBTYPE_OBJECT_RO =
bddAtom(OBJECT_RO_REC_ATOM);

public static final BddNode MAPPING_SUBTYPE_OBJECT_RO = bddAtom(OBJECT_RO_REC_ATOM);

public static final SemType VAL_READONLY = createComplexSemType(VT_INHERENTLY_IMMUTABLE,
BasicSubtype.from(BT_LIST, BDD_SUBTYPE_RO),
Expand All @@ -221,7 +218,6 @@ BT_CELL, bddAtom(ATOM_CELL_OBJECT_MEMBER_VISIBILITY)
);

public static final SemType INNER_READONLY = union(VAL_READONLY, UNDEF);

public static final CellAtomicType CELL_ATOMIC_INNER_RO = predefinedTypeEnv.cellAtomicInnerRO();
public static final TypeAtom ATOM_CELL_INNER_RO = predefinedTypeEnv.atomCellInnerRO();
public static final CellSemType CELL_SEMTYPE_INNER_RO = (CellSemType) basicSubtype(
Expand Down
44 changes: 19 additions & 25 deletions semtypes/src/main/java/io/ballerina/types/PredefinedTypeEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.types;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -43,9 +44,9 @@
import static io.ballerina.types.PredefinedType.MAPPING_SEMTYPE_OBJECT_MEMBER;
import static io.ballerina.types.PredefinedType.MAPPING_SEMTYPE_OBJECT_MEMBER_RO;
import static io.ballerina.types.PredefinedType.NEVER;
import static io.ballerina.types.PredefinedType.STRING;
import static io.ballerina.types.PredefinedType.UNDEF;
import static io.ballerina.types.PredefinedType.VAL;
import static io.ballerina.types.SemTypes.stringConst;
import static io.ballerina.types.TypeAtom.createTypeAtom;

/**
Expand Down Expand Up @@ -130,43 +131,36 @@ public static PredefinedTypeEnv getInstance() {
private TypeAtom atomMappingObjectMember;
private TypeAtom atomMappingObjectMemberRO;

// TODO: refactor
private void addInitializedCellAtom(CellAtomicType atom) {
int index = nextAtomIndex.getAndIncrement();
initializedCellAtoms.add(new InitializedTypeAtom<>(atom, index));
addInitializedAtom(initializedCellAtoms, atom);
}

private void addInitializedListAtom(ListAtomicType atom) {
int index = nextAtomIndex.getAndIncrement();
initializedListAtoms.add(new InitializedTypeAtom<>(atom, index));
addInitializedAtom(initializedListAtoms, atom);
}

private void addInitializedMapAtom(MappingAtomicType atom) {
int index = nextAtomIndex.getAndIncrement();
initializedMappingAtoms.add(new InitializedTypeAtom<>(atom, index));
addInitializedAtom(initializedMappingAtoms, atom);
}

private <E extends AtomicType> void addInitializedAtom(Collection<? super InitializedTypeAtom<E>> atoms, E atom) {
atoms.add(new InitializedTypeAtom<>(atom, nextAtomIndex.getAndIncrement()));
}

private int cellAtomIndex(CellAtomicType atom) {
for (InitializedTypeAtom<CellAtomicType> initializedCellAtom : initializedCellAtoms) {
if (initializedCellAtom.atomicType() == atom) {
return initializedCellAtom.index();
}
}
throw new IndexOutOfBoundsException();
return atomIndex(initializedCellAtoms, atom);
}

private int listAtomIndex(ListAtomicType atom) {
for (InitializedTypeAtom<ListAtomicType> initializedListAtom : initializedListAtoms) {
if (initializedListAtom.atomicType() == atom) {
return initializedListAtom.index();
}
}
throw new IndexOutOfBoundsException();
return atomIndex(initializedListAtoms, atom);
}

private int mappingAtomIndex(MappingAtomicType atom) {
for (InitializedTypeAtom<MappingAtomicType> initializedListAtom : initializedMappingAtoms) {
return atomIndex(initializedMappingAtoms, atom);
}

private <E extends AtomicType> int atomIndex(List<InitializedTypeAtom<E>> initializedAtoms, E atom) {
for (InitializedTypeAtom<E> initializedListAtom : initializedAtoms) {
if (initializedListAtom.atomicType() == atom) {
return initializedListAtom.index();
}
Expand Down Expand Up @@ -400,8 +394,8 @@ synchronized TypeAtom atomCellObjectMemberRO() {
synchronized CellAtomicType cellAtomicObjectMemberKind() {
if (cellAtomicObjectMemberKind == null) {
cellAtomicObjectMemberKind = CellAtomicType.from(
// FIXME: use singleton
STRING, CellAtomicType.CellMutability.CELL_MUT_NONE
Core.union(stringConst("field"), stringConst("method")),
CellAtomicType.CellMutability.CELL_MUT_NONE
);
addInitializedCellAtom(cellAtomicObjectMemberKind);
}
Expand All @@ -419,9 +413,9 @@ synchronized TypeAtom atomCellObjectMemberKind() {

synchronized CellAtomicType cellAtomicObjectMemberVisibility() {
if (cellAtomicObjectMemberVisibility == null) {
// FIXME: use singleton
cellAtomicObjectMemberVisibility = CellAtomicType.from(
STRING, CellAtomicType.CellMutability.CELL_MUT_NONE
Core.union(stringConst("public"), stringConst("private")),
CellAtomicType.CellMutability.CELL_MUT_NONE
);
addInitializedCellAtom(cellAtomicObjectMemberVisibility);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @param valueTy member type
* @param kind is member a field or a method
* @param visibility is member private or public
* @param immutable is member readonly. If this is set valueTy must be a subtype of readonly
* @since 2201.10.0
*/
public record Member(String name, SemType valueTy, Kind kind, Visibility visibility, boolean immutable) {
Expand Down Expand Up @@ -80,5 +81,4 @@ public Field field() {
};
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static io.ballerina.types.subtypedata.CellSubtype.cellContaining;

/**
* Represent {@code object-type-quals} in the spec
* Represent {@code object-type-quals} in the spec.
*
* @param isolated is object isolated
* @param readonly represent {@code class readonly}. Note this is used to determining "rest" part of the object
Expand All @@ -42,7 +42,7 @@
*/
public record ObjectQualifiers(boolean isolated, boolean readonly, NetworkQualifier networkQualifier) {

private final static ObjectQualifiers DEFAULT = new ObjectQualifiers(false, false, NetworkQualifier.None);
private static final ObjectQualifiers DEFAULT = new ObjectQualifiers(false, false, NetworkQualifier.None);

public static ObjectQualifiers defaultQualifiers() {
return DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ private SemType resolveTypeDesc(Context cx, Map<String, BLangNode> mod, BLangTyp
}
ObjectDefinition od = new ObjectDefinition();
Stream<Member> fieldStream = td.fields.stream().map(field -> {
Member.Visibility visibility = field.flagSet.contains(Flag.PUBLIC) ? Member.Visibility.Public :
Set<Flag> flags = field.flagSet;
Member.Visibility visibility = flags.contains(Flag.PUBLIC) ? Member.Visibility.Public :
Member.Visibility.Private;
SemType ty = resolveTypeDesc(cx, mod, defn, depth + 1, field.typeNode);
return new Member(field.name.value, ty, Member.Kind.Field, visibility, true);
return new Member(field.name.value, ty, Member.Kind.Field, visibility, flags.contains(Flag.READONLY));
});
Stream<Member> methodStream = td.getFunctions().stream().map(method -> {
Member.Visibility visibility = method.flagSet.contains(Flag.PUBLIC) ? Member.Visibility.Public :
Expand Down

0 comments on commit 76692ce

Please sign in to comment.