Eclipse: opcje, które ułatwiają pracę z kodem – część I / Eclipse: options that make coding easier – part I

Cześć! Chciałabym pokazać Wam kilka opcji w Eclipsie, które ułatwiają codzienną pracę z kodem: oszczędzają nasz czas i chronią przed przypadkowymi błędami. Na początek trzy opcje przydatne przy tworzeniu nowej klasy. Większość z Was pewnie od dawna je zna i stosuje (i bardzo dobrze :)), ale może ktoś jeszcze ich nie odkrył, a uzna je za pomocne.

***

Hi! I would like to show you a few options in Eclipse, that make everyday coding easier. Using them, saves our time and protects against typos. To begin with, three options useful by creating a new class. I guess most of you use them on a regular basis (and that’s good :)), but maybe someone has not discovered them yet and will consider them handy.

For English please scroll down.

Załóżmy, że mamy klasę Employee, w której deklarujemy kilka pól:

public class Employee {
   private String name;
   private String surname;
   private int age;
}

Możemy zaoszczędzić trochę czasu pozwalając, żeby Eclipse wygenerował akcesory i mutatory, czyli gettery i settery.

Gettery i settery

PPM (Prawy przycisk myszy) -> Source -> Generate Getters and Setters

lub Alt + Shift + S -> Generate Getters and Settersgetters_setters

Korzystając z tej opcji możemy nie tylko wybrać pola, dla których chcemy utworzyć metody, ale również rodzaj modyfikatora dostępu (public, protected, package etc) oraz miejsce pojawienia się metod w kodzie (tutaj za polem ‘age’).getters_setters2.pngPo chwili wybrane metody pojawiają się w naszej klasie.

public class Employee {
   private String name;
   private String surname;
   private int age;
 
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getSurname() {
      return surname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
}

Metoda toString()

W Eclipsie możliwe jest również wygenerowanie przesłoniętej metody toString().

PPM  -> Source / Alt+Shift+S -> Generate toString()..toString

W efekcie w klasie Employee mamy:

public class Employee {
   private String name;
   private String surname;
   private int age;  
   ...
  @Override
  public String toString() {
     return "Employee [name=" + name + ", surname=" + surname + ", age="
            + age + "]";
  }
}

Metody hashCode() i equals()

Potrzebne, gdy pracujemy z kolekcjami obiektów. I tu Eclipse może nas wspomóc:

PPM  -> Source / Alt+Shift+S -> Generate hashCode() or equals()..hashCodeWybrałam pola surname i name.

public class Employee {
   private String name;
   private String surname;
   private int age;

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      result = prime * result + ((surname == null) ? 0 : surname.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Employee other = (Employee) obj;
      if (name == null) {
         if (other.name != null)
            return false;
      } else if (!name.equals(other.name))
            return false;
      if (surname == null) {
         if (other.surname != null)
            return false;
      } else if (!surname.equals(other.surname))
            return false;
      return true;
      }
}

Tym sposobem, wystarczy porównać imiona i nazwiska pracowników, żeby stwierdzić, że nie są to identyczne obiekty. Nie ma potrzebny porównywania dodatkowo ich wieku.

***

Let’s assume we have the Employee class, in which we declere a few fields:

public class Employee {
   private String name;
   private String surname;
   private int age;
}

We can save time allowing Eclipse to generate accessors and mutators methods: getters and setters.

Getters and setters

Right Click -> Source -> Generate Getters and Setters

or Alt + Shift + S -> Generate Getters and Settersgetters_settersUsing this option we could not only choose fields, for which we want to create getters and setters. We are also able to choose type of access modifier (public, protected, package etc) and insertion point – the place where new methods should appear in the code. Here they would appear after ‘age’ field.getters_setters2.pngIn the moment, new methods are available in our class.

public class Employee {
   private String name;
   private String surname;
   private int age;
 
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getSurname() {
      return surname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
}

toString() method

In Eclipse there is also possible to generate custom toString() method.

Right Click-> Source / Alt+Shift+S -> Generate toString()..toStringAs a result, in class Employee we have:

public class Employee {
   private String name;
   private String surname;
   private int age;  
   ...
  @Override
  public String toString() {
     return "Employee [name=" + name + ", surname=" + surname + ", age="
            + age + "]";
  }
}

hashCode() and equals() methods

They are needed while we work with collection of objects. And here Eclipse comes handy again:

Right Click-> Source / Alt+Shift+S -> Generate hashCode() or equals()..hashCodeI have chosen only surname and name fields.

public class Employee {
   private String name;
   private String surname;
   private int age;

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      result = prime * result + ((surname == null) ? 0 : surname.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Employee other = (Employee) obj;
      if (name == null) {
         if (other.name != null)
            return false;
      } else if (!name.equals(other.name))
            return false;
      if (surname == null) {
         if (other.surname != null)
            return false;
      } else if (!surname.equals(other.surname))
            return false;
      return true;
      }
}

This way, only name and surname must be compared when you want to check if two Employee‘s objects are identical. There is no need to compare their age.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s