Using a HashMap with my object as a key - java

I want to make a simple note reminder app that uses hashMap when the date is the key and the value is the text.
I have a Panel class(GUI), the hash-table class (Reminder.java), and a "MyDateClass.java" that represent a date for my purposes.
My gui is made of 3 JComboBox (day,month,year), One text area and 2 buttons - "Save", "Load".
The 2 buttons in the GUI Panel:
butSave.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
MyDateClass chosenDate = new MyDateClass(cbYear.getSelectedIndex()+2013,cbMonth.getSelectedIndex()+1, cb.getSelectedIndex()+1);
if(!remind.isReminderExists(chosenDate)){
remind.save(chosenDate, tfReminder.getText());
System.out.println("reminder doesnt exists");
}}
});
butLoad.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.out.println("tryin to load");
MyDateClass chosenDate = new MyDateClass(cbYear.getSelectedIndex()+2013,cbMonth.getSelectedIndex()+1, cb.getSelectedIndex()+1);
if(remind.isReminderExists(chosenDate)){
remind.Load(chosenDate);
System.out.println("reminder exists");
}}
});
Reminder class:
public class Reminder {
Map<MyDateClass,String> reminderMap;
public Reminder(){
reminderMap = new HashMap<MyDateClass,String>();
}
public boolean isReminderExists(MyDateClass date){
return reminderMap.containsKey(date);
}
public void save(MyDateClass date, String Input){
System.out.println("Trying to save");
reminderMap.put(date, Input);
}
public void Load(MyDateClass date){
System.out.println("Trying to load");
String output;
output = reminderMap.get(date);
System.out.println(output);
}
So after i push the save button i get from the console:
Trying to save
reminder doesnt exists
But then i push the Load button for the same date and
if(remind.isReminderExists(chosenDate))
Isnt triggerd.
What might be the problem?
Do i need to override hashCode() and equals() ? I genereted them but i dont if and how to change the equals() (do i need to manipulate it to compare both dates? How do i do that if "this" refers to the reminder Object)

Try something like this.
#Override public boolean equals(Object o) {
if(o == this) return true;
if(!(o instanceof MyDateClass)) return false;
MyDateClass that = (MyDateClass) o;
// use == for primitives
// use .compare for primitive wrappers where available
// use .equals for objects
return this.ivar1 == that.ivar2 &&
this.ivar2 == that.ivar2; //etc...
}
// equal objects must have equal hash codes
#Override public int hashCode() {
int result = 17;
result = 31 * result + ivar1;
result = 31 * result + ivar2;
return result;
}
Write JUnits to test for reflexive, symmetric, transitive, and consistent results.

As I know your problem clearly, I think that this function always return false. Because date(parameter) and the object in reminderMap have difference reference. So they can not be equal.
public boolean isReminderExists(MyDateClass date){
return reminderMap.containsKey(date);
}
If you want to use containsKey function of HashMap, maybe you should use String or Number instead of MyDateClass. I mean you should convert object of MyDateClass to String value before inserting it into reminderMap.
public boolean isReminderExists(String date){
return reminderMap.containsKey(date);
}
Otherwise, you can implement your code as Hot Licks mention.

Related

Compare new elements in set with old elements in another set - Java

I have Set of Object which has existing values and getting a new Set values as an update. If the new Set does contain the old Object then I do nothing, if the new Set contains new Object then I want send create update and if the new Set doesn't contain an existing object then I want send a delete update.
Object has two fields :
private PreferenceType preferenceType;
private String preferenceValue;
Currently I am comparing the existing Set objects against new Set objects and if I don't find any existing object in new set then sending the delete update.
private void sendDeletePreferenceMessage(Set<AccountPreference> existingAccountPreferences, Set<AccountPreference> accountPreferencesFromRequest) {
int counter = 0;
for(AccountPreference accountPreference : existingAccountPreferences) {
for(AccountPreference accountPreference1: accountPreferencesFromRequest) {
if(accountPreference.getPreferenceType().equals(accountPreference1.getPreferenceType()) &&
accountPreference.getPreferenceValue().equals(accountPreference1.getPreferenceValue()))
counter++;
}
if(counter == 0) {
accPrefDeleteSender.send(accountPreference);
}
}
}
And also comparing the new set of objects against existing set of Objects to find the new updates that I want send as a create update
private void sendCreatePreferenceMessage(Set<AccountPreference> accountPreferencesFromRequest, Set<AccountPreference> existingAccountPreferences) {
int counter = 0;
for(AccountPreference accountPreference : accountPreferencesFromRequest) {
for(AccountPreference accountPreference1: existingAccountPreferences) {
if(accountPreference.getPreferenceType().equals(accountPreference1.getPreferenceType()) &&
accountPreference.getPreferenceValue().equals(accountPreference1.getPreferenceValue()))
counter++;
}
if(counter == 0) {
accPrefCreateSender.send(accountPreference);
}
}
}
This works perfectly but I believe this could be simplified in a better way. Any suggestion of doing this better!
You are using two AccountPreference Objects: one with id field, and another without id. I don't know if it's possible without using interfaces or abstract classes. Nevertheless, according to your comments it seems to me you're confused how to override Object.equals method, so I'll give you an example:
class MyObjectWithId {
private long id;
private String someField;
public String getSomeField() {
return someField;
}
#Override
public boolean equals(Object other){
if (this == other) return true;
if (other == null || !other.getClass().isInstance(MyObject.class)) return false;
MyObject myObject = (MyObject) other;
return this.getSomeField().equals(myObject.getSomeField());
}
}
class MyObject {
private String someField;
public String getSomeField() {
return someField;
}
#Override
public boolean equals(Object other){
if (this == other) return true;
if (other == null || !other.getClass().isInstance(MyObjectWithId.class)) return false;
MyObjectWithId myObjectWithId = (MyObjectWithId) other;
return this.getSomeField().equals(myObjectWithId.getSomeField());
}
}
As you can see, you can ask if the two different types of Object are equal (in this case, there isn't need for overriding hashCode).
Now you should be able to do the rest (regarding your original question about two Sets)
Set has a method boolean contains(Object o).
You don't need two for loops, just iterate through one Set and check if another Set contains() the object!
You also need to override the boolean equals(Object o) in the POJO!
class MyObject {
private PreferenceType preferenceType;
private String preferenceValue;
#Override
public boolean equals(Object o){
if (this == o)
return true;
if (o == null || o.getClass()o.getClass() != getClass())
return false;
if(this.getPreferenceType().equals(o.getPreferenceType()) && this.getPreferenceValue().equals(o.getPreferenceValue()))
return true;
return false;
}
}
}

Unable to set Object instance as selected item of JComboBox with Renderer

I have a DAOImplementation class with the method definition below.
#Override
public Registration getRegistrationInfoById(int aRegistrationId) {
String SQL = "{CALL getRegistrationInfoById(?)}";
Registration aRegistration = new Registration();
try (Connection con = DBUtil.getConnection(DBType.MYSQL);
CallableStatement cs = con.prepareCall(SQL);) {
cs.setInt(1, aRegistrationId);
try (ResultSet rs = cs.executeQuery();) {
while (rs.next()) {
int gradeLevel = Integer.parseInt(rs.getString(RegistrationTable.GRADELEVEL));
aRegistration.setGradeLevel(gradeLevel);
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getErrorCode() + "\n" + e.getMessage());
}
return aRegistration;
}//end of method
This returns an integer value of Grade Level (1,2,3,4,5,6,7...so on...) which I've verified because I tried printing the output returned by aRegistration.getGradeLevel();
Now my problem is with my JComboBox. I have set a ListCellRenderer for my JComboBox which holds all the GradeLevel values
public class JComboBoxRenderer_GradeLevel extends JLabel implements ListCellRenderer<Object> {
public JComboBoxRenderer_GradeLevel() {
this.setOpaque(true);
}
#Override
public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
} else {
this.setText("--");
}
if (isSelected) {
this.setBackground(Color.YELLOW);
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
}
}
And looks like this JComboBox as expected. (GradeLevel model is renderered to simply show an int value of gradelevel), ((GradeLevel) value).getGradelevel());returns an integer value.
I understand that even when JComboBox has its renderer that displays an integer value of GradeLevel by using ((GradeLevel)value).getGradeLevel(), the actual value on the JComboBox is still treated as instance of GradeLevel or object. But not a String or int.
So my problem is when I try to set the selected value to an int value, it won't change the selected value of the JComboBox. Nothing happens when I use setSelectedItem();
This is what I tried to do for the GUI.
//Grade Level
GradeLevelDaoImpl gldi = new GradeLevelDaoImpl();
List<GradeLevel> gradeLevels = gldi.getAllGradeLevelsInfo();
DefaultComboBoxModel gradeLevelModel = new DefaultComboBoxModel(gradeLevels.toArray());
jcmbGradeLevel.setModel(gradeLevelModel);
jcmbGradeLevel.setRenderer(new JComboBoxRenderer_GradeLevel());
jcmbGradeLevel.setSelectedIndex(-1);
GradeLevel gradeLevel = new GradeLevel();
gradeLevel.setGradelevel(registration.getGradeLevel());
jcmbGradeLevel.setSelectedItem(gradeLevel); //PROBLEM HERE, it doesn't change
JOptionPane displays this.
JOptionPane.showMessageDialog(null,"GradeLevel: "+gradeLevel);
JOptionPane.showMessageDialog(null,"GradeLevel: "+gradeLevel.getGradeLevel());
It doesn't seem to be able to compare the object I'm trying to set it to(gradeLevel) with the objects JComboBox has(gradeLevels). Notice the singular and plural.
How do I manipulate the types so that setSelectedItem() will match with what the JComboBox have?
Thanks.
If you want to do this by using different instances of the object, but with the same properties, then you need to override the class's equals and hashcode methods, so that the combination of properties are unique. This is very important, this is a relationship expectation that any object which is equal to another will have the same hashcode
This is a really quick example and I used by IDE's auto generation process (because I'm lazy), but, if your Registration class has other properties which should be considered when comparing to instances of the class, you will need to modify it to support them (again, any good IDE should be able to do this)
public class Registration {
private int gradeLevel;
public Registration(int gradeLevel) {
this.gradeLevel = gradeLevel;
}
public int getGradeLevel() {
return gradeLevel;
}
public void setGradeLevel(int gradeLevel) {
this.gradeLevel = gradeLevel;
}
#Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + this.gradeLevel;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Registration other = (Registration) obj;
if (this.gradeLevel != other.gradeLevel) {
return false;
}
return true;
}
}
Then using something like...
Registration a = new Registration(1);
Registration b = new Registration(1);
Registration c = new Registration(2);
System.out.println(a.equals(b));
System.out.println(a.equals(c));
System.out.println(b.equals(c));
will print...
true
false
false
which shows us that the code is working.
Once you get this setup, you should then be able to change the selected item by creating an instance of Registration, seeding it with the required properties and passing it to the JComboBox.
This is very important and very common concept used a lot within Java, well worth taking the time to learn and understand

How to create object based on boolean condition?

I have an Item object having 4 String fields and 3 boolean fields.
I have to construct this object based on the 3 boolean variables.
The target is whenever any one of the boolean variable is true we have to create the object having that/those boolean variable set.
If for any situation none of the boolean variables are true, we wont create the object.
I am using a COR to check whether any of the boolean fields will be set or not based on some business logic.
I was trying this with builder, but then I have to construct so many objects and later discard them when none of the boolean variables found true.
Can anyone have any better idea, to solve this kind of problem ?
Well thanks for the 2 delete flag for this question. Thank for the thoughts on this question as well.
I did something to achieve what I want. Which is quite flexible I believe. Only part if there is a dependency on If loop, but that is acceptable since Report class can have extra boolean so when that class is changed, it's builder should be touched to cater that change. Rest this is flexible which I wanted.
public class Report {
private String acftNo;
private Date plannedDate;
private String plannedStn;
private Integer mntncId;
private Set<String> capableStations;
private String routedStn;
private boolean isRoutedNEQPlannedStn; //Inconsistency type 1
private boolean isCapableAtPlannedStn; //Inconsistency type 2
private boolean isPlannedOrRoutedStationExists; //Inconsistency type 3/5
public Report(String acftNo, Integer mntncId) {
super();
this.acftNo = acftNo;
this.mntncId = mntncId;
}
public Report(String acftNo, Date plannedDate, String plannedStn,
Integer mntncId) {
super();
this.acftNo = acftNo;
this.plannedDate = plannedDate;
this.plannedStn = plannedStn;
this.mntncId = mntncId;
}
//setters and getters. Removed for space.
public static Report buildReport(Maintenance<?> task, Set<InconsistencyReport> enumSet) {
Report temp = new Report(task.getAssignment().getAircraftNumber(),task.getAssignment().getMntncScheduleDate(),
task.getAssignment().getStationCode(),task.getAssignment().getMntncId());
temp.setCapableStations(InconsistencyReport.getCapableStations(task));
for(InconsistencyReport ir : enumSet)
{
if(ir.compareTo(InconsistencyReport.ROUTED_STN_NEQ_PLANNED_STN)==0)
temp.setRoutedNEQPlannedStn(true);
if(ir.compareTo(InconsistencyReport.ITEM_NT_CAPABLE_AT_PLANNED_STN)==0)
temp.setCapableAtPlannedStn(true);
if(ir.compareTo(InconsistencyReport.NO_ROUTD_STN_ON_A_DATE)==0)
temp.setPlannedOrRoutedStationExists(true);
}
return temp;
}
}
calculateInconsitencyReport() method which will decide whether to create object or not.
public class InconsistencyReportChain {
public enum InconsistencyReport implements InconsistencyReportIface {
ROUTED_STN_NEQ_PLANNED_STN {
#Override
public boolean findInconsistency(Maintenance<?> task ) {
if(!validate(task))
return false;
//some logic
return true;
return false;
}
},
ITEM_NT_CAPABLE_AT_PLANNED_STN {
#Override
public boolean findInconsistency(Maintenance<?> task) {
if(!validate(task))
return false;
//some logic
return true;
return false;
}
},
NO_ROUTD_STN_ON_A_DATE {
#Override
public boolean findInconsistency(Maintenance<?> task) {
if(!validate(task))
return false;
//some logic
return true
return false;
}
};
#Override
public boolean validate(Maintenance<?> task) {
return !(null == task.getAssignment());
}
static Set<String> getCapableStations(Maintenance<?> task)
{
Set<String> capableStations = newHashSet();
if(task.getCapStationList() != null)
{
capableStations.addAll(Arrays.asList(task.getCapStationList().split(StringConstants.COMMA_SPLIT_REGEX)));
}
if(task.getCapStationClassList() != null)
{
Map<String, List<String>> stationClassMap = CacheManager.get(STN_CLASS.name());
List<String> stationClass = Arrays.asList(task.getCapStationClassList().split(StringConstants.COMMA_SPLIT_REGEX));
for(String stnClass : stationClass)
{
capableStations.addAll(stationClassMap.get(stnClass));
}
}
return capableStations;
}
}
public static Report calculateInconsitencyReport(Maintenance<?> task) {
Set<InconsistencyReport> enumSet = null;
for(InconsistencyReport iReport : InconsistencyReport.values())
{
if(iReport.findInconsistency(task))
{
if(null==enumSet)
enumSet = EnumSet.of(iReport);
else
enumSet.add(iReport);
}
}
if(null!= enumSet && enumSet.size() > 0)
return Report.buildReport(task,enumSet);
return null;
}
}
Helper Interface:
public interface InconsistencyReportIface {
public boolean findInconsistency(Maintenance<?> task );
public boolean validate(Maintenance<?> task );
}
Details of class logic is teared off because of security.
What is the problem? Just create your object when one of your booleans is true.
if(bool1 || bool2 || bool3) {
item = new Item(str1, str2, str3, str4, bool1, bool2, bool3);
}
From what I understand of your description:
a) you will have some bools that will determine wether you create a certain object or not.
b) you may have to include some more bools into the "check protocol"
c) you have to do this checking in a loop where
i/ you check for the bool variable
ii/ you check if the object had been created previously
I still don't quite get it yet, but.. that looks pretty straight forward to me. Let's say your bools are stored in a boolean array boolean[] bools and your strings in a string array String[] strings (which, btw, I don't know what they are used for). You are saying to check if every bool is true and then create an object based on that result.
boolean[] bools = new boolean[] { ... };
String[] strings = new String[] { ... };
boolean checks = false;
for(int i = 0; i<bools.length && !checks; i++)
checks = bools[i];
//so far we will have processed if any of the bools was false, which was your condition
if(checks)
Object object = new Object(); //create your desired object
I don't understand why you would need to check if the object has been constructed previously, though, so I didn't include it in my suggestion :P

Filter JFace ComboViewer by entering text

Im using a org.eclipse.jface.viewers.ComboViewer to show Names of some objects. If i have like hundreds of those objects it gets very difficult to find the right object very quickly.
By default the ComboViewer filters only for the first entered "Letter" but that is not the best solution for me, cause i might have Objects with names like:
"MyObject 1"
"MyObejct 2"
"MyObject 3"
and so on.
Currently im having this:
List<MyObject> myObjects = new ArrayList<MyObject>();
myObjects.add(MyObject("One"));
myObjects.add(MyObject("Two"));
myObjects.add(MyObject("ThreeOne"));
myObjects.add(MyObject("ThreeTwo"));
ComboViewer comboViewer = new ComboViewer(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
comboViewer.setContentProvider(ArrayContentProvider.getInstance());
comboViewer.setLabelProider(new LabelProvider(){
#Override
public String getText(final Object element){
if(element instanceof MyObject){
return MyObject.getName();
}
return super.getText(element);
}
comboViewer.setInput(myObjects);
Now i want to achieve the following.
When the User hits "O" then the MyObject "One" should be selected in the dropdownlist. When he hits "Th" then the MyObject "ThreeOne" should be selected in the dropdownlist.
When entering "ThreeT" then the MyObejct "ThreeTwo" should be selected and so on.
I hope it is clear what i mean with this.
I already tried org.eclipse.jface.fieldassist.AutoCompleteField but that does not always filter the correct Items. Cause there can be empty spaces in the MyObject's Name.
A simple Textfield with Autocompletion oder Autosuggest is not an option for me. I need this ComboViewer.
If you need any further infos please let me know.
EDIT:
I now have come pretty far.
comboViewer.getCombo().addKeyListener(new KeyListener() {
#Override
public void keyPressed(final KeyEvent e) {
}
#Override
public void keyReleased(final KeyEvent e) {
if (e.keyCode == SWT.ESC) {
myFilter.clearSearchString();
} else if (e.keyCode >= 97 && e.keyCode <= 122) {
myFilter.appendToSearchString(e.character);
} else if (e.keyCode >= 48 && e.keyCode <= 57) {
myFilter.appendToSearchString(e.character);
}
comboViewer.refresh();
}
});
The only thing I'm missing is: I'd like to see the entered Text somewhere in the ComboViewer.
Your ComboViewer supports filtering of data via the setFilters() or addFilter() methods. These methods expect ViewerFilter objects as arguments.
For each registered ViewerFilter object the select() method is called. The method returns true if the data should be shown and false if it should be filtered.
So add the filter once.
Then add a KeyListener to your
control updating the filter and trigger a viewer.refresh() afterwards.
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
public class PersonFilter extends ViewerFilter {
private String searchString;
public void setSearchText(String s) {
// ensure that the value can be used for matching
this.searchString = ".*" + s + ".*";
}
#Override
public boolean select(Viewer viewer,
Object parentElement,
Object element) {
if (searchString == null || searchString.length() == 0) {
return true;
}
Person p = (Person) element;
if (p.getFirstName().matches(searchString)) {
return true;
}
if (p.getLastName().matches(searchString)) {
return true;
}
return false;
}
}

Is it proper equals method for my class Java

As in subject: Is it proper equals method for my class Java? I have generated it automaticly by Eclipse I don't know if it makes vector.remove(pracownik) will work correctrly. Or is it wrong to generate it by Eclipse?
import java.util.Date;
import java.util.Vector;
public class Pracownik extends Osoba {
private String stanowisko;
private int pensja;
private Date dataZatrudnienia;
public Pracownik(Adres adres, String telefon, String imie, String nazwisko,
int id, Date dataUrodzenia, String stanowisko, int pensja,
Date dataZatrudnienia) {
super(adres, telefon, imie, nazwisko, id, dataUrodzenia);
this.stanowisko = stanowisko;
this.pensja = pensja;
this.dataZatrudnienia = dataZatrudnienia;
}
public String getStanowisko() {
return stanowisko;
}
public int getPensja() {
return pensja;
}
public Date getDataZatrudnienia() {
return dataZatrudnienia;
}
#Override
public String toString() {
return super.toString() + "\nstanowisko=" + stanowisko + "\npensja="
+ pensja + "\ndataZatrudnienia=" + dataZatrudnienia;
}
private static Vector<Pracownik> ekstensja = new Vector<Pracownik>();//kolekcja zawierajaca ekstensje
private static void dodajPracownik(Pracownik pracownik) { //metoda dodajac aobiekt do ekstensji
ekstensja.add(pracownik);
}
private static void usunPracownik(Pracownik pracownik) {//metoda usuwajaca obiekt z ekstensji
ekstensja.remove(pracownik);
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pracownik other = (Pracownik) obj;
if (dataZatrudnienia == null) {
if (other.dataZatrudnienia != null)
return false;
} else if (!dataZatrudnienia.equals(other.dataZatrudnienia))
return false;
if (pensja != other.pensja)
return false;
if (stanowisko == null) {
if (other.stanowisko != null)
return false;
} else if (!stanowisko.equals(other.stanowisko))
return false;
return true;
}
private static void pokazEkstensje(){ //wyswietlenie ekstensji przy pomocy petli for each
System.out.println("Ekstensja klasy Pracownik");
for(Pracownik pracownik: ekstensja)
System.out.println(pracownik);
System.out.println();
}
public static void main(String[] args){
Adres adres = new Adres("tara", "588 m.42", "03-422", "Warszawa");
Pracownik pracownik = new Pracownik(adres, "02-6451-4564", "Ala", "Kotowa", 323, new Date(), "szef", 14000, new Date()); //tworze pracownika
System.out.println(pracownik);//wyswietlam pracowanika
//tworze stazyste
Stazysta stazysta = new Stazysta(adres, "3232 9898", "frajer", "costam", 3232, new Date(), "podawanie kawy", 0, new Umowa(new Date(2010,10,5), new Date(2011,11,8)));
//wysswietlam stazyste
System.out.println(stazysta);
}
}
Generating the equals method using Eclipse is fine. It is important to make sure a field is included in the generation if, and only if, it affects logical equality of your objects.
When you override equals, you should also override hashCode.
In general, when overriding inherited methods you need to ensure the new methods conform to any rules stated in the superclass. The Object hashCode documentation states several rules, including "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."
The hashCode you inherit from Object follows that rule - if the equals method is also the one inherited from Object. It does not follow that rule when used with your equals method.
If you let Eclipse do its "Generate hashCode() and equals()" thing, it will get it right. If you write an equals method manually, you need to write your own hashCode to match.
As a practical matter, a class that does not follow the Object hashCode contract is a trap for future reuse. Hashed data structures, such as HashMap and HashSet, may fail to find an object that is actually present if it has a broken hashCode method. One lesson I've learned the hard way is that it is a mistake to depend on "I'll never use this that way.". It is much better to keep things safe as one goes along.
I am not expert on this but I believe double equals are comparing address rather than content of a variable. So, you may change it to .equals(). I hope someone with much more experience correct me if I am wrong.

Categories

Resources