Skip to content

Commit c18d5fe

Browse files
dfa1claude
andcommitted
Sonar: loop counters, record patterns, nested ternary, constants, redundant throws
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8c8a8cc commit c18d5fe

4 files changed

Lines changed: 36 additions & 26 deletions

File tree

‎cli/src/main/java/io/github/dfa1/typesafe/cli/Main.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.io.PrintStream;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22+
import java.util.Iterator;
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
26+
import java.util.NoSuchElementException;
2527

2628
/**
2729
* Command-line entry point, built as an uber-jar. Evaluates a single string {@code --state}
@@ -105,8 +107,7 @@ private static ApiKey loadApiKey(URI endpoint) throws IOException {
105107

106108
/** The evaluate-and-print flow, taking an already-built client so it's testable without a
107109
* network call. */
108-
static int run(TypeSafeClient client, Codec codec, ParsedArgs parsed, PrintStream out, PrintStream err)
109-
throws Exception {
110+
static int run(TypeSafeClient client, Codec codec, ParsedArgs parsed, PrintStream out, PrintStream err) {
110111
EvaluateRequest request = EvaluateRequest.of(Content.text(parsed.state()), parsed.model(), parsed.questions());
111112

112113
if (parsed.verbose()) {
@@ -155,19 +156,20 @@ static ParsedArgs parse(String[] args) {
155156
URI endpoint = null;
156157

157158
try {
158-
for (int i = 0; i < args.length; i++) {
159-
String flag = args[i];
159+
Iterator<String> it = List.of(args).iterator();
160+
while (it.hasNext()) {
161+
String flag = it.next();
160162

161163
switch (flag) {
162164
case "--verbose" -> verbose = true;
163165
case "--timing" -> timing = true;
164-
case "--state" -> state = args[++i];
165-
case "--model" -> model = new Model(args[++i]);
166-
case "--endpoint" -> endpoint = URI.create(args[++i]);
167-
case "--min" -> minSpecs.add(args[++i]);
168-
case "--print" -> printNames.add(args[++i]);
166+
case "--state" -> state = it.next();
167+
case "--model" -> model = new Model(it.next());
168+
case "--endpoint" -> endpoint = URI.create(it.next());
169+
case "--min" -> minSpecs.add(it.next());
170+
case "--print" -> printNames.add(it.next());
169171
case "--noul", "--choice", "--score" -> {
170-
String value = args[++i];
172+
String value = it.next();
171173
int eq = value.indexOf('=');
172174
String name = eq >= 0 ? value.substring(0, eq) : flag.substring(2);
173175
String rest = eq >= 0 ? value.substring(eq + 1) : value;
@@ -176,7 +178,7 @@ static ParsedArgs parse(String[] args) {
176178
default -> throw new IllegalArgumentException("Unknown flag: " + flag);
177179
}
178180
}
179-
} catch (ArrayIndexOutOfBoundsException e) {
181+
} catch (NoSuchElementException e) {
180182
throw new IllegalArgumentException("Missing value for " + args[args.length - 1]);
181183
}
182184

@@ -192,7 +194,7 @@ static ParsedArgs parse(String[] args) {
192194

193195
static String answerValue(EvaluateResponse response, String name) {
194196
return switch (response.answers().get(name)) {
195-
case Answer.Noul n -> String.valueOf(n.noul());
197+
case Answer.Noul(var noul) -> String.valueOf(noul);
196198
case Answer.Choice c -> c.choice();
197199
case Answer.Score s -> String.valueOf(s.score());
198200
case null -> throw new IllegalArgumentException("No such answer: " + name);
@@ -206,7 +208,7 @@ static List<String> minFailures(EvaluateResponse response, List<String> minSpecs
206208
String name = spec.substring(0, eq);
207209
double min = Double.parseDouble(spec.substring(eq + 1));
208210
double value = switch (response.answers().get(name)) {
209-
case Answer.Noul n -> n.noul();
211+
case Answer.Noul(var noul) -> noul;
210212
case Answer.Score s -> s.score();
211213
case null -> throw new IllegalArgumentException("No such answer: " + name);
212214
default -> throw new IllegalArgumentException(

‎client-http/src/main/java/io/github/dfa1/typesafe/client/http/HttpTransportResponse.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public Optional<String> header(String name) {
2222
// a record compares and prints an array by identity; compare the body by content instead
2323
@Override
2424
public boolean equals(Object o) {
25-
return o instanceof HttpTransportResponse r && statusCode == r.statusCode && headers.equals(r.headers)
26-
&& Arrays.equals(body, r.body);
25+
return o instanceof HttpTransportResponse(int s, var h, byte[] b) && statusCode == s && headers.equals(h)
26+
&& Arrays.equals(body, b);
2727
}
2828

2929
@Override

‎client-local/src/main/java/io/github/dfa1/typesafe/client/local/ClefEngine.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ private static Map<String, String> options(Question question) {
206206
options.put("true", c.getOrDefault("true", "The proposition is true or the answer is yes."));
207207
options.put("false", c.getOrDefault("false", "The proposition is false or the answer is no."));
208208
}
209-
case Question.Choice(Content ignored, Map<String, String> criteria) -> {
210-
criteria.entrySet().stream()
211-
.sorted(Map.Entry.comparingByKey(Comparator.comparing(k -> k.codePoints().toArray(), Arrays::compare)))
212-
.forEach(e -> options.put(e.getKey(), e.getValue()));
213-
}
209+
case Question.Choice(Content ignored, Map<String, String> criteria) -> criteria.entrySet().stream()
210+
.sorted(Map.Entry.comparingByKey(Comparator.comparing(k -> k.codePoints().toArray(), Arrays::compare)))
211+
.forEach(e -> options.put(e.getKey(), e.getValue()));
214212
case Question.Score(Content ignored, List<String> criteria) -> {
215213
for (int i = 0; i < criteria.size(); i++) {
216214
options.put(String.valueOf(i), criteria.get(i));

‎client-local/src/main/java/io/github/dfa1/typesafe/client/local/LayaEngine.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333
final class LayaEngine implements Engine {
3434

35+
private static final String MASK_TOKEN = "[MASK]";
3536
private static final int CHOICE = 0, SCORE = 1, NOUL = 2; // rl_common.QTYPES
3637
private static final int OPTION_TOKENS = 48;
3738

@@ -43,14 +44,14 @@ final class LayaEngine implements Engine {
4344
private final double[] temperature;
4445
private final Map<String, Double> temperatureByOptions;
4546

46-
private LayaEngine(OrtSession session, BpeTokenizer tokenizer, Map<String, Object> config, Model model) throws OrtException {
47+
private LayaEngine(OrtSession session, BpeTokenizer tokenizer, Map<String, Object> config, Model model) {
4748
this.session = session;
4849
this.model = model;
4950
this.tokenizer = tokenizer;
5051
this.cls = special("[CLS]");
5152
this.sep = special("[SEP]");
5253
this.pad = special("[PAD]");
53-
this.mask = special("[MASK]");
54+
this.mask = special(MASK_TOKEN);
5455
this.maxLen = ((Number) config.get("max_len")).intValue();
5556
this.headMaxLen = ((Number) config.get("head_max_len")).intValue();
5657
@SuppressWarnings("unchecked")
@@ -129,10 +130,10 @@ Sequence sequence(String state, Question question) {
129130
case Question.Noul ignored -> "noul";
130131
};
131132
Content instructions = question.instructions();
132-
long[] head = encode(type + " question: " + serialize(instructions).replace("[MASK]", " "));
133+
long[] head = encode(type + " question: " + serialize(instructions).replace(MASK_TOKEN, " "));
133134
List<long[]> opts = new ArrayList<>();
134135
for (String option : options(question)) {
135-
long[] text = encode(" " + option.replace("[MASK]", " "));
136+
long[] text = encode(" " + option.replace(MASK_TOKEN, " "));
136137
long[] withMarker = new long[1 + Math.min(OPTION_TOKENS, text.length)];
137138
withMarker[0] = mask;
138139
System.arraycopy(text, 0, withMarker, 1, withMarker.length - 1);
@@ -159,7 +160,7 @@ Sequence sequence(String state, Question question) {
159160
}
160161
ids.add(sep);
161162
int room = Math.max(0, maxLen - ids.size() - 1);
162-
long[] st = encode(state.replace("[MASK]", " "));
163+
long[] st = encode(state.replace(MASK_TOKEN, " "));
163164
for (int i = 0; i < Math.min(room, st.length); i++) {
164165
ids.add(st[i]);
165166
}
@@ -244,7 +245,16 @@ private static OnnxTensor bools(long[] mask, int n, int k) throws OrtException {
244245

245246
/** rl_agent_api: logits / temperature for this (type, option count) bucket, softmaxed over the real options. */
246247
private double[] probabilities(float[] logits, int k, int qtype) {
247-
String size = k <= 2 ? "2" : k <= 5 ? "3-5" : k <= 10 ? "6-10" : "11+";
248+
String size;
249+
if (k <= 2) {
250+
size = "2";
251+
} else if (k <= 5) {
252+
size = "3-5";
253+
} else if (k <= 10) {
254+
size = "6-10";
255+
} else {
256+
size = "11+";
257+
}
248258
String bucket = List.of("choice", "score", "noul").get(qtype) + ":" + size;
249259
double t = temperatureByOptions.getOrDefault(bucket, temperature[qtype]);
250260
float[] z = new float[k];

0 commit comments

Comments
 (0)