com.zaradai.gluon.cars.CarDB Maven / Gradle / Ivy
Show all versions of gluon-examples Show documentation
/**
* Copyright 2017 Zaradai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaradai.gluon.cars;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.*;
public enum CarDB {
INSTANCE;
private final Map> carByMake;
private final Map carByModel;
CarDB() {
carByModel = Maps.newHashMap();
carByMake = Maps.newHashMap();
hydrate();
}
private void hydrate() {
addCar(new Car("Audi", "A3"));
addCar(new Car("Ford", "Fiesta"));
addCar(new Car("Ford", "Focus"));
addCar(new Car("Ford", "Mondeo"));
addCar(new Car("Skoda", "Yeti"));
addCar(new Car("Volvo", "V40"));
}
private void addCar(Car car) {
carByMake.computeIfAbsent(car.getMake(), (key) -> Sets.newHashSet()).add(car);
carByModel.put(car.getModel(), car);
}
public Optional getByModel(String model) {
return Optional.ofNullable(carByModel.get(model));
}
public Set getByMake(String make) {
return Sets.newHashSet(carByMake.getOrDefault(make, Collections.EMPTY_SET));
}
}