Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Java 1.4 era 'for' loops #955

Open
wants to merge 70 commits into
base: next
Choose a base branch
from
Open

Conversation

Juiceman
Copy link
Contributor

Use Enhanced 'for' loops

Christophe and others added 3 commits February 8, 2023 15:48
This upgrade enables using the Gradle Linux Packaging Plugin (ospackage) in order to easily generate RPM and DEB packages for the Freenet REference Daemon on Linux OS.
We are looking for empty strings, so use .isEmpty()
Use Enhanced 'for' loops
@Bombe
Copy link
Contributor

Bombe commented Mar 18, 2024

While the enhanced for loops are nice, I would suggest to actually go one step further and use the Stream API, e.g.

buckets.stream().forEach(Bucket::free);

Of course that’s only possible in places where the method being called does not throw exceptions…

@Bombe Bombe self-requested a review March 18, 2024 11:13
@bertm
Copy link
Contributor

bertm commented Mar 18, 2024

If you're following Bombe's suggestion, please consider leaving out the .stream() (just using Iterable's forEach(Consumer)):

buckets.forEach(Bucket::free);

@hernic
Copy link
Contributor

hernic commented May 6, 2024

@Bombe could you review this one ?

ArneBab and others added 25 commits July 25, 2024 12:20
Hashes of libs distributed in dependencies.properties manually
checked against the verification-metadata.xml
baseName → archiveBaseName
archiveName → archiveFileName
destinationDir → destinationDirectory
Import from transifex
When using the gradle-daemon to build a project, the working directory
the script is executed in is the local directory of the gradle-daemon
running the script. By explicitely specifying the directory to execute
Git in, this will now work with and without gradle-daemon.
The default “jar” task a pre-defined input with compileJava’s output,
which includes the original, non-replaced Version.class. I have not
found a way to disable the pre-defined input, or modify it in a way that
would still allow me to add a Version.class from a different task’s
output.

What’s happening now is that there is a new task, “buildJar.” It is an
almost verbatim copy of what the “jar” task previously contained with
an added input (compileVersion’s output). This jar task does not have a
pre-defined input and as such only includes what is being specified.

The original jar task has been disabled and has been given the buildJar
task as dependency. That way the jar task can still be run from the
command line where it doesn’t do anything (because it’s disabled) but it
will require the buildJar task to be run which then does the actual
work.
Use Enhanced 'for' loops
# Conflicts:
#	src/freenet/clients/http/WelcomeToadlet.java
#	src/freenet/config/StringArrOption.java
#	src/freenet/support/PrioritizedSerialExecutor.java
@Bombe
Copy link
Contributor

Bombe commented Sep 23, 2024

Can you get this up to speed with current next, @Juiceman?

@Juiceman
Copy link
Contributor Author

Try it now please @Bombe

@Bombe
Copy link
Contributor

Bombe commented Sep 24, 2024

Ugh, why is Github showing all this cruft? If I diff locally, I get your changes only, which is what would I expect Github to do as well. This is really annyoing but I guess I’ll manage. I’ll take a look at it later today.

Copy link
Contributor

@Bombe Bombe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I hope I got everything… something in this branch seems to confuse Github to no end. Could you (after making a couple of changes) try to clean up this branch by rebasing it on current next? It would make further processing easier; but if it’s a problem, we still have the command line… 😄

for (int i=0,n=_buckets.size(); i<n;i++) {
_buckets.get(i).free();
for (Bucket bucket : _buckets) {
bucket.free();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a case like this, _buckets.forEach(Bucket::free); is preferable, because it better communicates the intent.

if (segmentFilter.checkFilter(salted)) {
return true;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing can be replaced with

if (segmentFilters.any(filter -> filter.checkFilter(salted))) {
    return true;
}

Again, this makes the intent clearer, because it does not require the reader to parse for- and if-constructs.

@@ -30,8 +30,7 @@ public SimpleEventProducer() {
/** Create a new SimpleEventProducer with the given listeners. */
public SimpleEventProducer(ClientEventListener[] cela) {
this();
for (int i = 0; i < cela.length; i++)
addEventListener(cela[i]);
for (ClientEventListener clientEventListener : cela) addEventListener(clientEventListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the original statement did not have braces, new statements will, please. 🙂

@@ -83,8 +82,7 @@ public synchronized ClientEventListener[] getEventListeners() {

/** Adds all listeners in the given array. */
public synchronized void addEventListeners(ClientEventListener[] cela) {
for (int i = 0; i < cela.length; i++)
addEventListener(cela[i]);
for (ClientEventListener clientEventListener : cela) addEventListener(clientEventListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here; braces, please.

@@ -841,8 +841,8 @@ public String toString() {
sb.append(element);
if (unparsedAttrs != null) {
int n = unparsedAttrs.length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think n is now unused and can be removed.

@@ -622,8 +620,7 @@ public void testPutAppend() {
+"bar"+SimpleFieldSet.MULTI_VALUE_CHAR
+"zoo";
SimpleFieldSet methodSFS = new SimpleFieldSet(true);
for (int i = 0 ; i < methodValues.length; i++)
methodSFS.putAppend(methodKey,methodValues[i]);
for (String methodValue : methodValues) methodSFS.putAppend(methodKey, methodValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces, please.

@@ -635,8 +632,7 @@ public void testGetAll() {
String methodKey = "foo.bar";
String[] methodValues = {"boo","bar","zoo"};
SimpleFieldSet methodSFS = new SimpleFieldSet(true);
for (int i = 0 ; i < methodValues.length; i++)
methodSFS.putAppend(methodKey,methodValues[i]);
for (String methodValue : methodValues) methodSFS.putAppend(methodKey, methodValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces, please.

@@ -708,8 +704,8 @@ public void testRemoveSubset() {
* @return true if there is the key
*/
private boolean isAKey(String[][] aStringPairsArray, String aPrefix, String aKey) {
for (int i=0; i<aStringPairsArray.length; i++)
if (aKey.equals(aPrefix+aStringPairsArray[i][0]))
for (String[] strings : aStringPairsArray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces, please.

@@ -141,8 +141,7 @@ public void testConversionWhileReading() throws IOException {
bucket.migrateToDisk();
byte[] readTo = new byte[16];
assertTrue(is.read(readTo, 0, 16) == 16);
for(int i=0;i<readTo.length;i++)
assertTrue(readTo[i] == 0);
for (byte b : readTo) assertTrue(b == 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces, please.

@@ -444,9 +444,9 @@ public int[] getMultipleIntParam(String name) {

// try parsing all values and put the valid Integers in a new list
List<Integer> intValueList = new ArrayList<Integer>();
for (int i = 0; i < valueList.size(); i++) {
for (String s : valueList) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more descriptive variable name than s would be appreciated. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants