3232
3333/**
3434 * @author kfuchsbe
35- * @param <E> the type of the elements of the tensor to build
35+ * @param <E>
36+ * the type of the elements of the tensor to build
3637 */
3738@ SuppressWarnings ("PMD.TooManyMethods" )
3839public abstract class AbstractTensorBuilder <E > implements TensorBuilder <E > {
3940
40- private final Set <Class <?>> dimensions ;
41- private final VerificationCallback <E > callback ;
42- private Context context = Context .empty ();
43-
44- public AbstractTensorBuilder (Set <Class <?>> dimensions , VerificationCallback <E > callback ) {
45- Preconditions .checkArgument (dimensions != null , "Argument '" + "dimensions" + "' must not be null!" );
46- Preconditions .checkArgument (callback != null , "Argument '" + "callback" + "' must not be null!" );
47- Coordinates .checkClassesRelations (dimensions );
48- this .dimensions = ImmutableSet .copyOf (dimensions );
49- this .callback = callback ;
50- }
51-
52- public AbstractTensorBuilder (Set <Class <?>> dimensions ) {
53- this (dimensions , new VerificationCallback <E >() {
54-
55- @ Override
56- public void verify (E scalar ) {
57- /* Nothing to do */
58- }
59- });
60- }
61-
62- /**
63- * Prepares to set a value at given position (a combined set of coordinates)
64- *
65- * @param entryPosition on which future value will be placed.
66- * @return builder object to be able to put Value in .
67- */
68- @ SuppressWarnings ( "PMD.ShortMethodName" )
69- public final OngoingPut < E > at ( Position entryPosition ) {
70- return new OngoingPut <E >( entryPosition , this );
71- }
72-
73- @ Override
74- public final void putAt ( E value , Position position ) {
75- Preconditions . checkNotNull ( value , "value must not be null!" );
76- Preconditions .checkNotNull (position , "position must not be null" );
77- Positions . assertConsistentDimensions (position , getDimensions () );
78- this .callback . verify ( value );
79- this .putItAt (value , position );
80- }
81-
82- protected abstract void putItAt ( E value , Position position );
83-
84- @ Override
85- public final void putAt ( E value , Object ... coordinates ) {
86- this . putAt (value , Position . of ( coordinates ));
87- }
88-
89- @ Override
90- public void putAt ( E value , Set <?> coordinates ) {
91- putAt (value , Position . of ( coordinates ));
92- }
93-
94- @ Override
95- public void putAllAt ( Tensor < E > tensor , Set <?> coordinates ) {
96- putAllAt (tensor , Position . of ( coordinates ));
97- }
98-
99- @ Override
100- public final void setTensorContext ( Context context ) {
101- Preconditions . checkNotNull ( context , " context must not be null" );
102- checkIfContextValid ( context );
103- this . context = context ;
104- }
105-
106- private void checkIfContextValid ( Context context2 ) {
107- Position contextPosition = context2 . getPosition ();
108- for (Class <?> oneDimensionClass : contextPosition .dimensionSet ()) {
109- if (dimensions .contains (oneDimensionClass )) {
110- throw new IllegalStateException ("Inconsistent state: " + oneDimensionClass
111- + " you are trying to put in to context is a BASE dimension of the tensor!" );
112- }
113- }
114- }
115-
116- @ SuppressWarnings ("PMD.ShortMethodName" )
117- public final OngoingPut <E > at (Set <?> coordinates ) {
118- return this .at (Position .of (coordinates ));
119- }
120-
121- @ SafeVarargs
122- @ SuppressWarnings ("PMD.ShortMethodName" )
123- public final OngoingPut <E > at (Object ... coordinates ) {
124- return this .at (Position .of (coordinates ));
125- }
126-
127- @ Override
128- public final void putAllAt (Tensor <E > tensor , Position position ) {
129- checkNotNull (tensor , "The tensor must not be null!" );
130- checkNotNull (position , "The position must not be null!" );
131- for (Entry <Position , E > entry : tensor .asMap ().entrySet ()) {
132- putAt (entry .getValue (), Positions .union (position , entry .getKey ()));
133- }
134- }
135-
136- @ Override
137- @ SafeVarargs
138- public final void putAllAt (Tensor <E > tensor , Object ... coordinates ) {
139- putAllAt (tensor , Position .of (coordinates ));
140- }
141-
142- public Set <Class <?>> getDimensions () {
143- return dimensions ;
144- }
145-
146- public Context getContext () {
147- return context ;
148- }
41+ private final Set <Class <?>> dimensions ;
42+ private final VerificationCallback <E > callback ;
43+ private Position context = Position .empty ();
44+
45+ public AbstractTensorBuilder (Set <Class <?>> dimensions , VerificationCallback <E > callback ) {
46+ Preconditions .checkArgument (dimensions != null , "Argument '" + "dimensions" + "' must not be null!" );
47+ Preconditions .checkArgument (callback != null , "Argument '" + "callback" + "' must not be null!" );
48+ Coordinates .checkClassesRelations (dimensions );
49+ this .dimensions = ImmutableSet .copyOf (dimensions );
50+ this .callback = callback ;
51+ }
52+
53+ public AbstractTensorBuilder (Set <Class <?>> dimensions ) {
54+ this (dimensions , new VerificationCallback <E >() {
55+
56+ @ Override
57+ public void verify (E scalar ) {
58+ /* Nothing to do */
59+ }
60+ });
61+ }
62+
63+ /**
64+ * Prepares to set a value at given position (a combined set of coordinates)
65+ *
66+ * @param entryPosition
67+ * on which future value will be placed .
68+ * @return builder object to be able to put Value in.
69+ */
70+ @ SuppressWarnings ( "PMD.ShortMethodName" )
71+ public final OngoingPut <E > at ( Position entryPosition ) {
72+ return new OngoingPut < E >( entryPosition , this );
73+ }
74+
75+ @ Override
76+ public final void putAt ( E value , Position position ) {
77+ Preconditions .checkNotNull (value , "value must not be null! " );
78+ Preconditions . checkNotNull (position , "position must not be null" );
79+ Positions . assertConsistentDimensions ( position , this .dimensions );
80+ this .callback . verify (value );
81+ this . putItAt ( value , position );
82+ }
83+
84+ protected abstract void putItAt ( E value , Position position );
85+
86+ @ Override
87+ public final void putAt (E value , Object ... coordinates ) {
88+ this . putAt ( value , Position . of ( coordinates ));
89+ }
90+
91+ @ Override
92+ public void putAt (E value , Set <?> coordinates ) {
93+ putAt ( value , Position . of ( coordinates ));
94+ }
95+
96+ @ Override
97+ public void putAllAt (Tensor < E > tensor , Set <?> coordinates ) {
98+ putAllAt ( tensor , Position . of ( coordinates ));
99+ }
100+
101+ @ Override
102+ public void context ( Position context ) {
103+ Preconditions . checkNotNull ( context , "context must not be null" );
104+ checkIfContextValid ( context ) ;
105+ this . context = context ;
106+ }
107+
108+ private void checkIfContextValid ( Position context2 ) {
109+ for (Class <?> oneDimensionClass : context2 .dimensionSet ()) {
110+ if (dimensions .contains (oneDimensionClass )) {
111+ throw new IllegalStateException ("Inconsistent state: " + oneDimensionClass
112+ + " you are trying to put in to context is a BASE dimension of the tensor!" );
113+ }
114+ }
115+ }
116+
117+ @ SuppressWarnings ("PMD.ShortMethodName" )
118+ public final OngoingPut <E > at (Set <?> coordinates ) {
119+ return this .at (Position .of (coordinates ));
120+ }
121+
122+ @ SafeVarargs
123+ @ SuppressWarnings ("PMD.ShortMethodName" )
124+ public final OngoingPut <E > at (Object ... coordinates ) {
125+ return this .at (Position .of (coordinates ));
126+ }
127+
128+ @ Override
129+ public final void putAllAt (Tensor <E > tensor , Position position ) {
130+ checkNotNull (tensor , "The tensor must not be null!" );
131+ checkNotNull (position , "The position must not be null!" );
132+ for (Entry <Position , E > entry : tensor .asMap ().entrySet ()) {
133+ putAt (entry .getValue (), Positions .union (position , entry .getKey ()));
134+ }
135+ }
136+
137+ @ Override
138+ @ SafeVarargs
139+ public final void putAllAt (Tensor <E > tensor , Object ... coordinates ) {
140+ putAllAt (tensor , Position .of (coordinates ));
141+ }
142+
143+ public Set <Class <?>> dimensions () {
144+ return dimensions ;
145+ }
146+
147+ public Position context () {
148+ return this . context ;
149+ }
149150}
0 commit comments