Skip to content

Commit

Permalink
Merge pull request #510 from scireum/feature/sbi/SequencedCollections
Browse files Browse the repository at this point in the history
Simplifies code by utilizing sequenced collection helpers
  • Loading branch information
sabieber authored Dec 5, 2023
2 parents d6632ff + c1deaf1 commit 6489732
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/Classpath.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private String buildRelativePath(File reference, File child) {
List<String> path = new ArrayList<>();
File iter = child;
while (iter != null && !Objects.equals(iter, reference)) {
path.add(0, iter.getName());
path.addFirst(iter.getName());
iter = iter.getParentFile();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/async/Promises.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static <V> Promise<List<V>> parallel(List<Promise<V>> list) {
}

if (list.size() == 1) {
return list.get(0).map(Collections::singletonList);
return list.getFirst().map(Collections::singletonList);
}

// Create a list with the correct length
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/async/Tasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private long computeWaitTime() {
}

long now = System.currentTimeMillis();
return Math.max(0, schedulerQueue.get(0).waitUntil - now);
return Math.max(0, schedulerQueue.getFirst().waitUntil - now);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sirius/kernel/cache/ManagedCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ protected void runEviction() {
protected void updateStatistics() {
usesHistory.add(getUses());
if (usesHistory.size() > MAX_HISTORY) {
usesHistory.remove(0);
usesHistory.removeFirst();
}
hitRateHistory.add(getHitRate());
if (hitRateHistory.size() > MAX_HISTORY) {
hitRateHistory.remove(0);
hitRateHistory.removeFirst();
}
hits.reset();
misses.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ public void handle(Incident incident) throws Exception {
unique = false;
}
}
incidents.add(0, incident);
incidents.addFirst(incident);
numIncidents.inc();
if (unique) {
numUniqueIncidents.inc();
}
while (incidents.size() > maxErrors) {
incidents.remove(incidents.size() - 1);
incidents.removeLast();
}
}
}

@Override
public void handleLogMessage(LogMessage msg) {
synchronized (messages) {
messages.add(0, msg);
messages.addFirst(msg);
numLogMessages.inc();
while (messages.size() > maxMsg) {
messages.remove(messages.size() - 1);
messages.removeLast();
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/sirius/kernel/health/console/TimerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ private String extractScope(List<String> parameters) {
return "";
}

String scope = parameters.get(0);
parameters.remove(0);
return scope;
return parameters.removeFirst();
}

private boolean extractForceParameter(List<String> parameters) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/nls/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private Block endBlock(List<Block> blocks, Block currentBlock, int index) {
next.replacementFound = true;
}
currentBlock = blocks.get(blocks.size() - 2);
blocks.remove(blocks.size() - 1);
blocks.removeLast();
return currentBlock;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/tokenizer/Lookahead.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void consume(int numberOfItems) {
}
while (numberOfItems-- > 0) {
if (!itemBuffer.isEmpty()) {
itemBuffer.remove(0);
itemBuffer.removeFirst();
} else {
if (endReached) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sirius/kernel/tokenizer/ParseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private ParseException(String message, List<ParseError> errors) {
*/
public static ParseException create(List<ParseError> errors) {
if (errors.size() == 1) {
return new ParseException(errors.get(0).getMessage(), errors);
return new ParseException(errors.getFirst().getMessage(), errors);
} else if (errors.size() > 1) {
return new ParseException(String.format("%d errors occured. First: %s",
errors.size(),
errors.get(0).getMessage()), errors);
errors.getFirst().getMessage()), errors);
} else {
return new ParseException("An unknown error occured", errors);
}
Expand Down
30 changes: 14 additions & 16 deletions src/main/java/sirius/kernel/xml/AbstractStructuredOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected ElementType getCurrentType() {
if (nesting.isEmpty()) {
return ElementType.UNKNOWN;
}
return nesting.get(0).getType();
return nesting.getFirst().getType();
}

/**
Expand All @@ -96,16 +96,16 @@ public boolean isCurrentObjectEmpty() {
if (nesting.isEmpty()) {
return true;
}
return nesting.get(0).isEmpty();
return nesting.getFirst().isEmpty();
}

@Override
public StructuredOutput beginArray(String name) {
startArray(name);
if (!nesting.isEmpty()) {
nesting.get(0).setEmpty(false);
nesting.getFirst().setEmpty(false);
}
nesting.add(0, new Element(ElementType.ARRAY, name));
nesting.addFirst(new Element(ElementType.ARRAY, name));

return this;
}
Expand Down Expand Up @@ -224,9 +224,9 @@ protected String transformToStringRepresentation(Object value) {
public StructuredOutput beginObject(String name, Attribute... attributes) {
startObject(name, attributes);
if (!nesting.isEmpty()) {
nesting.get(0).setEmpty(false);
nesting.getFirst().setEmpty(false);
}
nesting.add(0, new Element(ElementType.OBJECT, name));
nesting.addFirst(new Element(ElementType.OBJECT, name));
return this;
}

Expand Down Expand Up @@ -295,12 +295,11 @@ public StructuredOutput endArray() {
if (nesting.isEmpty()) {
throw new IllegalArgumentException("Invalid result structure. No array to close");
}
Element e = nesting.get(0);
nesting.remove(0);
if (e.getType() != ElementType.ARRAY) {
Element element = nesting.removeFirst();
if (element.getType() != ElementType.ARRAY) {
throw new IllegalArgumentException("Invalid result structure. No array to close");
}
endArray(e.getName());
endArray(element.getName());
return this;
}

Expand All @@ -309,12 +308,11 @@ public StructuredOutput endObject() {
if (nesting.isEmpty()) {
throw new IllegalArgumentException("Invalid result structure. No object to close");
}
Element e = nesting.get(0);
nesting.remove(0);
if (e.getType() != ElementType.OBJECT) {
Element element = nesting.removeFirst();
if (element.getType() != ElementType.OBJECT) {
throw new IllegalArgumentException("Invalid result structure. No object to close");
}
endObject(e.getName());
endObject(element.getName());
return this;
}

Expand All @@ -336,7 +334,7 @@ public StructuredOutput property(String name, Object data) {
} else {
writeProperty(name, data);
}
nesting.get(0).setEmpty(false);
nesting.getFirst().setEmpty(false);
return this;
}

Expand Down Expand Up @@ -373,7 +371,7 @@ public StructuredOutput amountProperty(@Nonnull String name,
writeAmountProperty(name, amount.toString(numberFormat).asString());
}

nesting.get(0).setEmpty(false);
nesting.getFirst().setEmpty(false);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/kernel/xml/XMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void endElement(String uri, String localName, String name) throws SAXExce
// Delegate to active handlers and deletes them if they are finished...
activeHandlers.removeIf(handler -> handler.endElement(name));

currentPath.remove(currentPath.size() - 1);
currentPath.removeLast();
}

@Override
Expand Down

0 comments on commit 6489732

Please sign in to comment.