Skip to content

Commit dcfa397

Browse files
authored
Merge pull request #23 from tensorics/context-becomes-position-final
Final stage for irradication of context
2 parents a10cd8a + fe9534a commit dcfa397

File tree

7 files changed

+181
-246
lines changed

7 files changed

+181
-246
lines changed

src/java/org/tensorics/core/lang/Tensorics.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.tensorics.core.math.ExtendedField;
3333
import org.tensorics.core.quantity.ImmutableQuantifiedValue;
3434
import org.tensorics.core.quantity.QuantifiedValue;
35-
import org.tensorics.core.tensor.Context;
3635
import org.tensorics.core.tensor.ImmutableTensor;
3736
import org.tensorics.core.tensor.Position;
3837
import org.tensorics.core.tensor.Shape;

src/java/org/tensorics/core/tensor/BroadcastedTensorView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Shape shape() {
100100
}
101101

102102
@Override
103-
public Context context() {
103+
public Position context() {
104104
return originalTensor.context();
105105
}
106106

src/java/org/tensorics/core/tensor/Context.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/java/org/tensorics/core/tensor/ImmutableTensor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public T get(Position position) {
185185
}
186186

187187
@Override
188-
public Context context() {
189-
return Context.of(context);
188+
public Position context() {
189+
return this.context;
190190
}
191191

192192
@Override

src/java/org/tensorics/core/tensor/Position.java

Lines changed: 122 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -35,123 +35,130 @@
3535
import com.google.common.collect.Interners;
3636

3737
/**
38-
* Defines the position of a value within a tensor in the N-dimensional coordinate space.
38+
* Defines the position of a value within a tensor in the N-dimensional
39+
* coordinate space.
3940
*
4041
* @author agorzaws
4142
*/
42-
// TODO temporary non-final! - for the ongoing refactoring of Context
43-
public class Position implements Serializable {
44-
45-
private static final long serialVersionUID = 1L;
46-
47-
private static final Interner<Position> INTERNER = Interners.newWeakInterner();
48-
49-
/*
50-
* NOTE: This has to be after the cachedPositions initialization, because the 'of' method uses it!
51-
*/
52-
private static final Position EMPTY_POSITION = Position.createFrom(Collections.emptySet());
53-
54-
private ImmutableSet<?> coordinates;
55-
56-
protected Position(Set<?> coordinates) {
57-
this.coordinates = ImmutableSet.copyOf(coordinates);
58-
for (Object oneCoordinate : coordinates) {
59-
if (oneCoordinate instanceof Position) {
60-
throw new IllegalArgumentException("A position is contained in the collection of coordinates."
61-
+ "This is most-probably a programming mistake and therefore not allowed.");
62-
}
63-
}
64-
}
65-
66-
public static Position of(Set<?> coordinates) {
67-
return createFrom(requireValidCoordinates(coordinates));
68-
}
69-
70-
private static Position createFrom(Set<?> coordinates) {
71-
return INTERNER.intern(new Position(coordinates));
72-
}
73-
74-
public static Position empty() {
75-
return EMPTY_POSITION;
76-
}
77-
78-
@SafeVarargs
79-
public static Position of(Object... coordinates) {
80-
return createFrom(requireValidCoordinates(ImmutableMultiset.copyOf(coordinates)));
81-
}
82-
83-
/**
84-
* @deprecated use coordinates() instead
85-
*/
86-
@Deprecated
87-
public Set<?> getCoordinates() {
88-
return coordinates();
89-
}
90-
91-
public <CS> CS coordinateFor(Class<CS> dimension) {
92-
return Coordinates.firstCoordinateOfTyp(coordinates, dimension);
93-
}
94-
95-
public Set<?> coordinates() {
96-
return coordinates;
97-
}
98-
99-
/**
100-
* Retrieves the dimensions of this position (i.e. the type of the containing coordinates).
101-
* <p>
102-
* <b>NOTE!</b> These dimensions may differ from the ones kept in the parent tensor!.
103-
*
104-
* @return the types of the coordinates
105-
*/
106-
public Set<Class<?>> dimensionSet() {
107-
return Coordinates.getDimensionsFrom(coordinates);
108-
}
109-
110-
/**
111-
* Checks if the position is consistent with the given dimensions. Conformity means that the position contains
112-
* exactly one coordinate for each dimension in the given set of dimensions (classes of coordinates).
113-
*
114-
* @param dimensions the dimensions for which conformity has to be checked.
115-
* @return {@code true} if the position is conform, {@code false} if not.
116-
*/
117-
public boolean isConsistentWith(Set<Class<?>> dimensions) {
118-
Preconditions.checkArgument(dimensions != null, "Argument 'dimensions' must not be null!");
119-
return Positions.areDimensionsConsistentWithCoordinates(dimensions, this);
120-
}
121-
122-
@Override
123-
public int hashCode() {
124-
final int prime = 31;
125-
int result = 1;
126-
result = prime * result + ((coordinates == null) ? 0 : coordinates.hashCode());
127-
return result;
128-
}
129-
130-
@Override
131-
public boolean equals(Object obj) {
132-
if (this == obj) {
133-
return true;
134-
}
135-
if (obj == null) {
136-
return false;
137-
}
138-
if (! (obj instanceof Position)) { // TODO: this should be changed again to standard of getClass() after context refactoring
139-
return false;
140-
}
141-
Position other = (Position) obj;
142-
if (coordinates == null) {
143-
if (other.coordinates != null) {
144-
return false;
145-
}
146-
} else if (!coordinates.equals(other.coordinates)) {
147-
return false;
148-
}
149-
return true;
150-
}
151-
152-
@Override
153-
public String toString() {
154-
return "Position [coordinates=" + coordinates + "]";
155-
}
43+
public final class Position implements Serializable {
44+
45+
private static final long serialVersionUID = 1L;
46+
47+
private static final Interner<Position> INTERNER = Interners.newWeakInterner();
48+
49+
/*
50+
* NOTE: This has to be after the cachedPositions initialization, because
51+
* the 'of' method uses it!
52+
*/
53+
private static final Position EMPTY_POSITION = Position.createFrom(Collections.emptySet());
54+
55+
private ImmutableSet<?> coordinates;
56+
57+
protected Position(Set<?> coordinates) {
58+
this.coordinates = ImmutableSet.copyOf(coordinates);
59+
for (Object oneCoordinate : coordinates) {
60+
if (oneCoordinate instanceof Position) {
61+
throw new IllegalArgumentException("A position is contained in the collection of coordinates."
62+
+ "This is most-probably a programming mistake and therefore not allowed.");
63+
}
64+
}
65+
}
66+
67+
public static Position of(Set<?> coordinates) {
68+
return createFrom(requireValidCoordinates(coordinates));
69+
}
70+
71+
private static Position createFrom(Set<?> coordinates) {
72+
return INTERNER.intern(new Position(coordinates));
73+
}
74+
75+
public static Position empty() {
76+
return EMPTY_POSITION;
77+
}
78+
79+
@SafeVarargs
80+
public static Position of(Object... coordinates) {
81+
return createFrom(requireValidCoordinates(ImmutableMultiset.copyOf(coordinates)));
82+
}
83+
84+
/**
85+
* @deprecated use coordinates() instead
86+
*/
87+
@Deprecated
88+
public Set<?> getCoordinates() {
89+
return coordinates();
90+
}
91+
92+
public <CS> CS coordinateFor(Class<CS> dimension) {
93+
return Coordinates.firstCoordinateOfTyp(coordinates, dimension);
94+
}
95+
96+
public Set<?> coordinates() {
97+
return coordinates;
98+
}
99+
100+
/**
101+
* Retrieves the dimensions of this position (i.e. the type of the
102+
* containing coordinates).
103+
* <p>
104+
* <b>NOTE!</b> These dimensions may differ from the ones kept in the parent
105+
* tensor!.
106+
*
107+
* @return the types of the coordinates
108+
*/
109+
public Set<Class<?>> dimensionSet() {
110+
return Coordinates.getDimensionsFrom(coordinates);
111+
}
112+
113+
/**
114+
* Checks if the position is consistent with the given dimensions.
115+
* Conformity means that the position contains exactly one coordinate for
116+
* each dimension in the given set of dimensions (classes of coordinates).
117+
*
118+
* @param dimensions
119+
* the dimensions for which conformity has to be checked.
120+
* @return {@code true} if the position is conform, {@code false} if not.
121+
*/
122+
public boolean isConsistentWith(Set<Class<?>> dimensions) {
123+
Preconditions.checkArgument(dimensions != null, "Argument 'dimensions' must not be null!");
124+
return Positions.areDimensionsConsistentWithCoordinates(dimensions, this);
125+
}
126+
127+
@Override
128+
public int hashCode() {
129+
final int prime = 31;
130+
int result = 1;
131+
result = prime * result + ((coordinates == null) ? 0 : coordinates.hashCode());
132+
return result;
133+
}
134+
135+
@Override
136+
public boolean equals(Object obj) {
137+
if (this == obj) {
138+
return true;
139+
}
140+
if (obj == null) {
141+
return false;
142+
}
143+
if (!(obj instanceof Position)) { // TODO: this should be changed again
144+
// to standard of getClass() after
145+
// context refactoring
146+
return false;
147+
}
148+
Position other = (Position) obj;
149+
if (coordinates == null) {
150+
if (other.coordinates != null) {
151+
return false;
152+
}
153+
} else if (!coordinates.equals(other.coordinates)) {
154+
return false;
155+
}
156+
return true;
157+
}
158+
159+
@Override
160+
public String toString() {
161+
return "Position [coordinates=" + coordinates + "]";
162+
}
156163

157164
}

0 commit comments

Comments
 (0)