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

1 immutability of results #12

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions library/src/main/java/com/tomaytotomato/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import com.tomaytotomato.usecase.FindCity;
import com.tomaytotomato.usecase.FindCountry;
import com.tomaytotomato.usecase.FindState;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* This class provides simple lookups for Country, State and City information.
Expand Down Expand Up @@ -148,7 +151,7 @@ public Optional<Country> findCountryByISO3Code(String iso3Code) {
}

public List<Country> findAllCountries() {
return countries;
return Collections.unmodifiableList(countries);
}

/**
Expand All @@ -174,9 +177,9 @@ public List<Country> findAllCountriesByStateName(String stateName) {
return stateNameToStatesMap.get(stateName).stream().map(state -> findCountryById(state.getCountryId()))
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
} else {
return new ArrayList<>();
return Collections.emptyList();
}
}

Expand All @@ -197,9 +200,9 @@ public List<State> findAllStatesByStateName(String stateName) {
}
stateName = textNormaliser.normalise(stateName);
if (stateNameToStatesMap.containsKey(stateName)) {
return stateNameToStatesMap.get(stateName);
return Collections.unmodifiableList(stateNameToStatesMap.get(stateName));
} else {
return new ArrayList<>();
return Collections.emptyList();
}
}

Expand All @@ -210,9 +213,9 @@ public List<State> findAllStatesByStateCode(String stateCode) {
}
stateCode = textNormaliser.normalise(stateCode);
if (stateCodeToStatesMap.containsKey(stateCode)) {
return stateCodeToStatesMap.get(stateCode);
return Collections.unmodifiableList(stateCodeToStatesMap.get(stateCode));
} else {
return new ArrayList<>();
return Collections.emptyList();
}
}

Expand All @@ -226,7 +229,8 @@ public Optional<City> findCityById(Integer id) {

@Override
public List<City> findAllCities() {
return cityNameToCitiesMap.values().stream().flatMap(List::stream).toList();
return cityNameToCitiesMap.values().stream().flatMap(List::stream).collect(
Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}

@Override
Expand All @@ -236,9 +240,9 @@ public List<City> findAllCitiesByCityName(String cityName) {
}
cityName = textNormaliser.normalise(cityName);
if (cityNameToCitiesMap.containsKey(cityName)) {
return cityNameToCitiesMap.get(cityName);
return Collections.unmodifiableList(cityNameToCitiesMap.get(cityName));
} else {
return new ArrayList<>();
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

/**
* Provides search functionality to find a Country, State or City based on text input
*
*/
public class SearchLocationService implements SearchLocation {

Expand Down
Loading