City class that describe a city as an object (used to retreive a city from the database and use it in the java code)

This commit is contained in:
BGbaderguet 2020-11-09 19:56:16 +01:00
parent 8f336520c9
commit bd5e6bb9cc

View File

@ -0,0 +1,82 @@
package fr.romanet.vj.apps.myweather.bo;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
//This class is used in order to represent a city
@Entity
final public class City implements Serializable
{
//Class CityComparator that implements Comparator interface in order to compare two cities
public static final class CityComparator implements Comparator<City>
{
/**
* Compare function that compare two city by their name
* @param o1 The first city to compare
* @param o2 Second city to compare with the first
* @return
*/
@Override
public int compare(City o1, City o2)
{
return o1.nameCity.compareTo(o2.nameCity);
}
}
/**
* The primary key of the city in our datbase which is automatically incremented
*/
@PrimaryKey(autoGenerate = true)
public int id;
/**
* The name of the city
*/
@NonNull
public String nameCity;
/**
* Constructor of the city class
* @param nameCity The name of the new city
*/
public City(@NonNull String nameCity)
{
id = 0;
this.nameCity = nameCity;
}
/**
* Equals function that return true or false weither both name are same and both object belongs to City class
* @param o The object to compare
* @return true or false
*/
@Override
public boolean equals(Object o) {
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
City city1 = (City) o;
return nameCity.equals(city1.nameCity);
}
/**
* hashCode function
* @return Clé de hashage (int)
*/
@Override
public int hashCode()
{
return Objects.hash(nameCity);
}
}