Use String indexOf(char) and lastIndexOf(char) where possible

Closes gh-11416
This commit is contained in:
Andy Wilkinson 2019-07-04 17:44:40 +01:00
parent 6a777a7f9b
commit 4b2a116fa7
7 changed files with 11 additions and 11 deletions

View File

@ -127,9 +127,9 @@ class StringSequenceTests {
@Test @Test
void indexOfStringShouldReturnIndexOf() { void indexOfStringShouldReturnIndexOf() {
StringSequence sequence = new StringSequence("aabbaacc"); StringSequence sequence = new StringSequence("aabbaacc");
assertThat(sequence.indexOf("a")).isEqualTo(0); assertThat(sequence.indexOf('a')).isEqualTo(0);
assertThat(sequence.indexOf("b")).isEqualTo(2); assertThat(sequence.indexOf('b')).isEqualTo(2);
assertThat(sequence.subSequence(2).indexOf("a")).isEqualTo(2); assertThat(sequence.subSequence(2).indexOf('a')).isEqualTo(2);
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ public class SampleApplication {
if (!argument.startsWith("--spring.profiles.active=")) { if (!argument.startsWith("--spring.profiles.active=")) {
throw new IllegalArgumentException("Invalid argument " + argument); throw new IllegalArgumentException("Invalid argument " + argument);
} }
int index = args[0].indexOf("="); int index = args[0].indexOf('=');
String profile = argument.substring(index + 1); String profile = argument.substring(index + 1);
System.out.println("I haz been run with profile(s) '" + profile + "'"); System.out.println("I haz been run with profile(s) '" + profile + "'");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ public class SampleApplication {
if (!argument.startsWith("--spring.profiles.active=")) { if (!argument.startsWith("--spring.profiles.active=")) {
throw new IllegalArgumentException("Invalid argument " + argument); throw new IllegalArgumentException("Invalid argument " + argument);
} }
int index = args[0].indexOf("="); int index = args[0].indexOf('=');
String profile = argument.substring(index + 1); String profile = argument.substring(index + 1);
System.out.println("I haz been run with profile(s) '" + profile + "'"); System.out.println("I haz been run with profile(s) '" + profile + "'");
} }

View File

@ -108,7 +108,7 @@ class BindFailureAnalyzerTests {
MutablePropertySources sources = context.getEnvironment().getPropertySources(); MutablePropertySources sources = context.getEnvironment().getPropertySources();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
for (String pair : environment) { for (String pair : environment) {
int index = pair.indexOf("="); int index = pair.indexOf('=');
String key = (index > 0) ? pair.substring(0, index) : pair; String key = (index > 0) ? pair.substring(0, index) : pair;
String value = (index > 0) ? pair.substring(index + 1) : ""; String value = (index > 0) ? pair.substring(index + 1) : "";
map.put(key.trim(), value.trim()); map.put(key.trim(), value.trim());

View File

@ -119,7 +119,7 @@ class BindValidationFailureAnalyzerTests {
MutablePropertySources sources = context.getEnvironment().getPropertySources(); MutablePropertySources sources = context.getEnvironment().getPropertySources();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
for (String pair : environment) { for (String pair : environment) {
int index = pair.indexOf("="); int index = pair.indexOf('=');
String key = (index > 0) ? pair.substring(0, index) : pair; String key = (index > 0) ? pair.substring(0, index) : pair;
String value = (index > 0) ? pair.substring(index + 1) : ""; String value = (index > 0) ? pair.substring(index + 1) : "";
map.put(key.trim(), value.trim()); map.put(key.trim(), value.trim());

View File

@ -91,7 +91,7 @@ class UnboundConfigurationPropertyFailureAnalyzerTests {
MutablePropertySources sources = context.getEnvironment().getPropertySources(); MutablePropertySources sources = context.getEnvironment().getPropertySources();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
for (String pair : environment) { for (String pair : environment) {
int index = pair.indexOf("="); int index = pair.indexOf('=');
String key = (index > 0) ? pair.substring(0, index) : pair; String key = (index > 0) ? pair.substring(0, index) : pair;
String value = (index > 0) ? pair.substring(index + 1) : ""; String value = (index > 0) ? pair.substring(index + 1) : "";
map.put(key.trim(), value.trim()); map.put(key.trim(), value.trim());

View File

@ -50,7 +50,7 @@ class CityServiceImpl implements CityService {
} }
String country = ""; String country = "";
int splitPos = name.lastIndexOf(","); int splitPos = name.lastIndexOf(',');
if (splitPos >= 0) { if (splitPos >= 0) {
country = name.substring(splitPos + 1); country = name.substring(splitPos + 1);