Skip to content

Commit 33d25bc

Browse files
committed
#20: Fix LGTM warnings.
1 parent 699fba0 commit 33d25bc

File tree

11 files changed

+75
-64
lines changed

11 files changed

+75
-64
lines changed

prngine/src/main/java/io/jenetics/prngine/KISS32Random.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public void setSeed(final byte[] seed) {
340340
}
341341

342342
@Override
343-
public void setSeed(final long seed) {
343+
public synchronized void setSeed(final long seed) {
344344
setSeed(PRNG.seedBytes(seed, SEED_BYTES));
345345
}
346346

prngine/src/main/java/io/jenetics/prngine/KISS64Random.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private void step() {
345345
* @throws IllegalArgumentException if the given seed is shorter than
346346
* {@link #SEED_BYTES}
347347
*/
348-
public void setSeed(final byte[] seed) {
348+
public synchronized void setSeed(final byte[] seed) {
349349
if (_state != null) _state.setSeed(seed);
350350
}
351351

prngine/src/main/java/io/jenetics/prngine/LCG64ShiftRandom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ public void setSeed(final byte[] seed) {
521521
}
522522

523523
@Override
524-
public void setSeed(final long seed) {
524+
public synchronized void setSeed(final long seed) {
525525
setSeed(PRNG.seedBytes(seed, SEED_BYTES));
526526
}
527527

prngine/src/main/java/io/jenetics/prngine/MT19937_32Random.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private TLRandom(final long seed) {
9090
}
9191

9292
@Override
93-
public void setSeed(final long seed) {
93+
public synchronized void setSeed(final long seed) {
9494
if (_sentry != null) {
9595
throw new UnsupportedOperationException(
9696
"The 'setSeed(long)' method is not supported " +
@@ -328,7 +328,7 @@ public void setSeed(final byte[] seed) {
328328
}
329329

330330
@Override
331-
public void setSeed(final long seed) {
331+
public synchronized void setSeed(final long seed) {
332332
if (_state != null) _state.setSeed(seed);
333333
}
334334

prngine/src/main/java/io/jenetics/prngine/MT19937_64Random.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private TLRandom(final long seed) {
9191
}
9292

9393
@Override
94-
public void setSeed(final long seed) {
94+
public synchronized void setSeed(final long seed) {
9595
if (_sentry != null) {
9696
throw new UnsupportedOperationException(
9797
"The 'setSeed(long)' method is not supported " +
@@ -331,7 +331,7 @@ public void setSeed(final byte[] seed) {
331331
}
332332

333333
@Override
334-
public void setSeed(final long seed) {
334+
public synchronized void setSeed(final long seed) {
335335
if (_state != null) _state.setSeed(seed);
336336
}
337337

prngine/src/main/java/io/jenetics/prngine/PRNG.java

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,15 @@ public static int nextInt(
186186
}
187187

188188
final int value;
189-
190-
if (origin < bound) {
191-
int n = bound - origin;
192-
if (n > 0) {
193-
value = random.nextInt(n) + origin;
194-
} else {
195-
int r;
196-
do {
197-
r = random.nextInt();
198-
} while (r < origin || r >= bound);
199-
value = r;
200-
}
189+
int n = bound - origin;
190+
if (n > 0) {
191+
value = random.nextInt(n) + origin;
201192
} else {
202-
value = random.nextInt();
193+
int r;
194+
do {
195+
r = random.nextInt();
196+
} while (r < origin || r >= bound);
197+
value = r;
203198
}
204199

205200
return value;
@@ -230,22 +225,20 @@ public static long nextLong(
230225
}
231226

232227
long value = random.nextLong();
233-
if (origin < bound) {
234-
long n = bound - origin, m = n - 1;
235-
if ((n & m) == 0L) {
236-
value = (value & m) + origin;
237-
} else if (n > 0L) {
238-
for (long u = value >>> 1;
239-
u + m - (value = u % n) < 0L;
240-
u = random.nextLong() >>> 1)
241-
{
242-
}
243-
244-
value += origin;
245-
} else {
246-
while (value < origin || value >= bound) {
247-
value = random.nextLong();
248-
}
228+
long n = bound - origin, m = n - 1;
229+
if ((n & m) == 0L) {
230+
value = (value & m) + origin;
231+
} else if (n > 0L) {
232+
for (long u = value >>> 1;
233+
u + m - (value = u % n) < 0L;
234+
u = random.nextLong() >>> 1)
235+
{
236+
}
237+
238+
value += origin;
239+
} else {
240+
while (value < origin || value >= bound) {
241+
value = random.nextLong();
249242
}
250243
}
251244

prngine/src/main/java/io/jenetics/prngine/Random32.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public int nextInt() {
124124
}
125125

126126
@Override
127-
public void setSeed(final long seed) {
127+
public synchronized void setSeed(final long seed) {
128128
if (_sentry != null) {
129129
throw new UnsupportedOperationException(
130130
"The 'setSeed(long)' method is not supported."

prngine/src/main/java/io/jenetics/prngine/Random64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public long nextLong() {
137137
}
138138

139139
@Override
140-
public void setSeed(final long seed) {
140+
public synchronized void setSeed(final long seed) {
141141
if (_sentry != null) {
142142
throw new UnsupportedOperationException(
143143
"The 'setSeed(long)' method is not supported."

prngine/src/main/java/io/jenetics/prngine/XOR32ShiftRandom.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public static enum Shift {
8989
public int shift(int x, final Param param) {
9090
x ^= x << param.a;
9191
x ^= x >>> param.b;
92-
return x^x << param.c;
92+
x ^= x << param.c;
93+
return x ;
9394
}
9495
},
9596

@@ -108,7 +109,8 @@ public int shift(int x, final Param param) {
108109
public int shift(int x, final Param param) {
109110
x ^= x << param.c;
110111
x ^= x >>> param.b;
111-
return x^x << param.a;
112+
x ^= x << param.a;
113+
return x;
112114
}
113115
},
114116

@@ -127,7 +129,8 @@ public int shift(int x, final Param param) {
127129
public int shift(int x, final Param param) {
128130
x ^= x >>> param.a;
129131
x ^= x << param.b;
130-
return x^x >>> param.c;
132+
x ^= x >>> param.c;
133+
return x;
131134
}
132135
},
133136

@@ -146,7 +149,8 @@ public int shift(int x, final Param param) {
146149
public int shift(int x, final Param param) {
147150
x ^= x >>> param.c;
148151
x ^= x << param.b;
149-
return x^x >>> param.a;
152+
x ^= x >>> param.a;
153+
return x;
150154
}
151155
},
152156

@@ -165,7 +169,8 @@ public int shift(int x, final Param param) {
165169
public int shift(int x, final Param param) {
166170
x ^= x << param.a;
167171
x ^= x << param.c;
168-
return x^x >>> param.b;
172+
x ^= x >>> param.b;
173+
return x;
169174
}
170175
},
171176

@@ -184,7 +189,8 @@ public int shift(int x, final Param param) {
184189
public int shift(int x, final Param param) {
185190
x ^= x << param.c;
186191
x ^= x << param.a;
187-
return x^x >>> param.b;
192+
x ^= x >>> param.b;
193+
return x;
188194
}
189195
},
190196

@@ -203,7 +209,8 @@ public int shift(int x, final Param param) {
203209
public int shift(int x, final Param param) {
204210
x ^= x >>> param.a;
205211
x ^= x >>> param.c;
206-
return x^x << param.b;
212+
x ^= x << param.b;
213+
return x;
207214
}
208215
},
209216

@@ -222,7 +229,8 @@ public int shift(int x, final Param param) {
222229
public int shift(int x, final Param param) {
223230
x ^= x >>> param.c;
224231
x ^= x >>> param.a;
225-
return x^x << param.b;
232+
x ^= x << param.b;
233+
return x;
226234
}
227235
};
228236

@@ -710,7 +718,7 @@ private static int toSafeSeed(final int seed) {
710718
}
711719

712720
@Override
713-
public void setSeed(final long seed) {
721+
public synchronized void setSeed(final long seed) {
714722
_x = toSafeSeed((int)seed);
715723
}
716724

prngine/src/main/java/io/jenetics/prngine/XOR64ShiftRandom.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public static enum Shift {
8989
public long shift(long x, final Param param) {
9090
x ^= x << param.a;
9191
x ^= x >>> param.b;
92-
return x^x << param.c;
92+
x ^= x << param.c;
93+
return x;
9394
}
9495
},
9596

@@ -108,7 +109,8 @@ public long shift(long x, final Param param) {
108109
public long shift(long x, final Param param) {
109110
x ^= x << param.c;
110111
x ^= x >>> param.b;
111-
return x^x << param.a;
112+
x ^= x << param.a;
113+
return x;
112114
}
113115
},
114116

@@ -127,7 +129,8 @@ public long shift(long x, final Param param) {
127129
public long shift(long x, final Param param) {
128130
x ^= x >>> param.a;
129131
x ^= x << param.b;
130-
return x^x >>> param.c;
132+
x ^= x >>> param.c;
133+
return x;
131134
}
132135
},
133136

@@ -146,7 +149,8 @@ public long shift(long x, final Param param) {
146149
public long shift(long x, final Param param) {
147150
x ^= x >>> param.c;
148151
x ^= x << param.b;
149-
return x^x >>> param.a;
152+
x ^= x >>> param.a;
153+
return x;
150154
}
151155
},
152156

@@ -165,7 +169,8 @@ public long shift(long x, final Param param) {
165169
public long shift(long x, final Param param) {
166170
x ^= x << param.a;
167171
x ^= x << param.c;
168-
return x^x >>> param.b;
172+
x ^= x >>> param.b;
173+
return x;
169174
}
170175
},
171176

@@ -184,7 +189,8 @@ public long shift(long x, final Param param) {
184189
public long shift(long x, final Param param) {
185190
x ^= x << param.c;
186191
x ^= x << param.a;
187-
return x^x >>> param.b;
192+
x ^= x >>> param.b;
193+
return x;
188194
}
189195
},
190196

@@ -203,7 +209,8 @@ public long shift(long x, final Param param) {
203209
public long shift(long x, final Param param) {
204210
x ^= x >>> param.a;
205211
x ^= x >>> param.c;
206-
return x^x << param.b;
212+
x ^= x << param.b;
213+
return x;
207214
}
208215
},
209216

@@ -222,7 +229,8 @@ public long shift(long x, final Param param) {
222229
public long shift(long x, final Param param) {
223230
x ^= x >>> param.c;
224231
x ^= x >>> param.a;
225-
return x^x << param.b;
232+
x ^= x << param.b;
233+
return x;
226234
}
227235
};
228236

@@ -830,7 +838,7 @@ private static long toSafeSeed(final long seed) {
830838
}
831839

832840
@Override
833-
public void setSeed(final long seed) {
841+
public synchronized void setSeed(final long seed) {
834842
_x = toSafeSeed((int)seed);
835843
}
836844

@@ -847,7 +855,7 @@ public String toString() {
847855
@Override
848856
public int hashCode() {
849857
int hash = 31;
850-
hash += 17*_x + 37;
858+
hash += 17*Long.hashCode(_x) + 37;
851859
hash += 17*_param.hashCode() + 37;
852860

853861
return hash;

0 commit comments

Comments
 (0)