Skip to content

Commit 3ede274

Browse files
committed
keep track of field types instead of checking later
1 parent add24dc commit 3ede274

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

‎common-tools/cnuphys/magfield/src/main/java/cnuphys/magfield/CompositeField.java‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
@SuppressWarnings("serial")
1212
public class CompositeField extends ArrayList<IMagField> implements IMagField {
1313

14+
boolean hasTorus;
15+
boolean hasSolenoid;
16+
1417
/**
1518
* Checks whether the field has been set to always return zero.
1619
*
@@ -43,6 +46,9 @@ public boolean add(IMagField field) {
4346
}
4447
}
4548

49+
hasSolenoid = field instanceof Solenoid;
50+
hasTorus = field instanceof Torus;
51+
4652
return super.add(field);
4753
}
4854

@@ -69,13 +75,7 @@ public String getName() {
6975
* @return <code>true</code> if we have a torus
7076
*/
7177
public boolean hasTorus() {
72-
for (IMagField field : this) {
73-
if (field instanceof Torus) {
74-
return true;
75-
}
76-
}
77-
78-
return false;
78+
return hasTorus;
7979
}
8080

8181
/**
@@ -84,13 +84,7 @@ public boolean hasTorus() {
8484
* @return <code>true</code> if we have a solenoid
8585
*/
8686
public boolean hasSolenoid() {
87-
for (IMagField field : this) {
88-
if (field instanceof Solenoid) {
89-
return true;
90-
}
91-
}
92-
93-
return false;
87+
return hasSolenoid;
9488
}
9589

9690
/**

0 commit comments

Comments
 (0)