11/*
2- Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2+ Copyright 2019 The TensorFlow Authors. All Rights Reserved.
33
4- Licensed under the Apache License, Version 2.0 (the "License");
5- you may not use this file except in compliance with the License.
6- You may obtain a copy of the License at
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
77
8- http://www.apache.org/licenses/LICENSE-2.0
8+ http://www.apache.org/licenses/LICENSE-2.0
99
10- Unless required by applicable law or agreed to in writing, software
11- distributed under the License is distributed on an "AS IS" BASIS,
12- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- See the License for the specific language governing permissions and
14- limitations under the License.
15- =======================================================================
16- */
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ =======================================================================
16+ */
1717package org .tensorflow .ndarray .impl ;
1818
19- import java .util .Iterator ;
2019import org .tensorflow .ndarray .NdArray ;
2120import org .tensorflow .ndarray .NdArraySequence ;
2221import org .tensorflow .ndarray .Shape ;
2322import org .tensorflow .ndarray .impl .dimension .DimensionalSpace ;
2423
24+ import java .util .Iterator ;
25+ import java .util .Objects ;
26+
2527@ SuppressWarnings ("unchecked" )
2628public abstract class AbstractNdArray <T , U extends NdArray <T >> implements NdArray <T > {
2729
30+ protected final DimensionalSpace dimensions ;
31+
32+ protected AbstractNdArray (DimensionalSpace dimensions ) {
33+ this .dimensions = dimensions ;
34+ }
35+
2836 public abstract U slice (long position , DimensionalSpace dimensions );
2937
3038 public DimensionalSpace dimensions () {
@@ -39,7 +47,7 @@ public Shape shape() {
3947 @ Override
4048 public NdArraySequence <U > scalars () {
4149 // negative if this array is a scalar, should be handled in `elements(dimIdx)`
42- return (NdArraySequence <U >)elements (shape ().numDimensions () - 1 );
50+ return (NdArraySequence <U >) elements (shape ().numDimensions () - 1 );
4351 }
4452
4553 @ Override
@@ -55,11 +63,7 @@ public boolean equals(Object obj) {
5563 if (!(obj instanceof NdArray )) {
5664 return false ;
5765 }
58- return slowEquals ((NdArray <?>)obj );
59- }
60-
61- protected AbstractNdArray (DimensionalSpace dimensions ) {
62- this .dimensions = dimensions ;
66+ return slowEquals ((NdArray <?>) obj );
6367 }
6468
6569 protected void slowCopyTo (NdArray <T > array ) {
@@ -77,16 +81,19 @@ protected int slowHashCode() {
7781 }
7882
7983 protected boolean slowEquals (NdArray <?> array ) {
80- if (!shape ().equals (array .shape ())) { // this guarantees also that we have the same number of scalar values
84+ if (!shape ()
85+ .equals (
86+ array .shape ())) { // this guarantees also that we have the same number of scalar values
8187 return false ;
8288 }
83- for (Iterator <? extends NdArray <?>> thisIter = scalars ().iterator (), otherIter = array .scalars ().iterator (); thisIter .hasNext ();) {
84- if (!thisIter .next ().getObject ().equals (otherIter .next ().getObject ())) {
89+ for (Iterator <? extends NdArray <?>> thisIter = scalars ().iterator (),
90+ otherIter = array .scalars ().iterator ();
91+ thisIter .hasNext (); ) {
92+ // Use Object.equals to handle nulls.
93+ if (!Objects .equals (thisIter .next ().getObject (), otherIter .next ().getObject ())) {
8594 return false ;
8695 }
8796 }
8897 return true ;
8998 }
90-
91- protected final DimensionalSpace dimensions ;
9299}
0 commit comments