Pages

Tuesday 29 January 2013

my Collections test and something else (key features of Java)

I study and test some basic technologies. I created a few enums with default access modifiers (they can be used in scope of the current package only).

enum Sex {MALE, FEMALE, SHEMALE}
This Sex enum is used to hold the gender of a Person. Of course, it's a only joke.

enum Employment {EMPLOYED, SELFEMPLOYED, UNEMPLOYED};
This enum is used to hold the employment status of a Person.

My public class Person definition:

public class Person implements Serializable, Comparable {
    private long ID;
    private String name;
    private String surname;
    private int age;
    private Sex sex = Sex.FEMALE;
    private Employment vocation = Employment.UNEMPLOYED;
    private File userPic;
...

Serializable - in order to be saved into the file
Class Person implements interface Comparable (override a method: public int compareTo(Person o){} ) in order to be passed into any Collection class. (and override a method public boolean equals(Object obj) as well).

I added several Constructors for this class:

1st - Constructor with all arguments:

    Person(String name, String surname, int age, Sex sex, Employment vocation){
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.sex = sex;
        this.vocation = vocation;
    }

2nd - Constructor based on the same type:

    Person(Person p){
        this.name = p.name;
        this.surname = p.surname;
        this.age = p.age;
        this.sex = p.sex;
        this.vocation = p.vocation;   
    }

3rd - Default Constructor that invokes another constructor:

    Person(){
     this("unknown", "unknown", 0, Sex.FEMALE ,Employment.UNEMPLOYED);
    }

For fun, i added a factory method which produces an instance of this class. This method looks interesting (i think) because it produces an instance with a random gender of a person. I get the random value of the Sex enum.

    public  Person burnPerson(String name, String surname){
        Sex[] sa = Sex.values();
        int size = sa.length;
        int random =  new Random().nextInt(size);
        Sex sx = sa[random];
        return new Person(name,surname,0, sx);
    }

I override the toString() method such a way:

    @Override
    public String toString() {
        return (this.getClass().getSimpleName() + ": "
                + this.name + " "
                + this.surname + " "
                + this.age +" "
                + this.sex + " "
                + this.vocation);
    }

and i implement Comparable interface like that:

   @Override
    public int compareTo(Person o) {
        return this.toString().compareToIgnoreCase(o.toString());
    }

I implemented setter and getter methods as well. And this class can save (write) and restore itself using serialization (can be written into ObjectOutputStream and restored from ObjectInputStream):

    public void write(ObjectOutputStream os) throws IOException {
        os.writeObject(this);
    }
   
    public static Person read(ObjectInputStream is) throws IOException, ClassNotFoundException {
        Object o;
        if ((o = is.readObject()) instanceof Person) {
           return (Person) o;
        }else {
           return null;
        }
    }

I override equals(Object obj) and hashCode() methods as well.

Using Person class:

List<Person> pList = new ArrayList<>();
pList.add(new Person("Alex", "Banson", 35, Sex.MALE, Employment.SELFEMPLOYED));
pList.add(new Person("Sasha", "Lee", 30, Sex.FEMALE, Employment.UNEMPLOYED));
pList.add(new Person().burnPerson("John", "Johnson")); //we get random gender of an instance.

Sorting (using compareTo() method of the class, force all persons to be employed and print out their state:

        Collections.sort(pList); //natural (alphabetical) sorting
        for(Person p : pList){
            p.setVocation(Employment.EMPLOYED);
            System.out.println(p);
        }

Sorting using Comparator (i created the class which implements Comparator interface):

    class AgeSort implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getAge() - o2.getAge();
        }
    }
and then:

        AgeSort aSort = new AgeSort(); //sorted by Age
        Collections.sort(pList, aSort);

No comments:

Post a Comment