2

So, I am trying to use an enumerated data type as parameter in the place of an object being passed in. I know that a simple switch statement would work but that doesn't really seem elegant to me. I have searched and found that enums can also have actions attached to them but I'm not so clear how to use it in this case or if it is even possible, or if i am just really tired. let me try to use code to explain what I'm asking.

First I have a class with certain fields of other objects that I am basically trying to use the enums to reference. In this case I have a method that acts on one of the fields of trees, because their are multiple trees the method needs to know which tree to act on.

public class bstContactManage()
{
// fields of other objects
BST searchTreeFirstName = new BST(new ComparatorObjOne);
BST searchTreeLastName = new BST(new ComparatorObjTwo);
// and so on and so forth

public boolean modify(Contact contactToFind, BST ToFindIn, String newContactInfo)
{
  Contact contUpdate = new Contact(ContactToFind)//save for readdition to tree
  contUpdate.update(newContactInfo);
  toFindIn.remove(contactToFind);
  if(toFindIn.add(contUpdate)) return true;
  else return false;
}
}

what I'm wondering or more or less pondering is how to replace the BST parameter with a an enum i know i could use a switch statement but that doesn't seem any more effective maybe more elegant than passing it an int value and letting it go wild!

so is there a way to get method to look something like

public boolean modify(Contact contactToFind, Enum BSTType, String newContactInfo)
{
  Contact contUpdate = new Contact(ContactToFind)//save for readdition to tree
  contUpdate.update(newContactInfo);
  BSTType.remove(contactToFind);
  if(BSTType.add(contUpdate)) return true;
  else return false;
}

most of my question stems from the fact that an object such as

bstContactManage man = new bstContactManage()

will be instantiated in another class, and therefore it isn't safe or doesn't seem proper to me to do something like man.modify(contactIn, man.searchTreeFirstName, "String");

update:

so for more clarification i have another method find which searches a given BST, and currently i am implementing it like this

public List<Contact> find(BinarySearchTree treeUsed, String findThis)
{
//create a new contact with all fields being the same, find is dependent and comparator on tree;
Contact tempContact = new Contact(findThis, findThis, findThis);
return treeUsed.getEntry(tempContact); // where getEntry returns a list of all matching contacts
}

I could do something like

public List<Contact> find(EnumField field, String findThis)
{
BST treeUsed;
switch(Field){
   case FIRST:
     treeUsed = this.searchTreeFirstName;
     break;
   cast LAST:
     treeUsed = this.searchTreeLastName;
     break;
Contact tempContact = new Contact(findThis, findThis, findThis);
return treeUsed.getEntry(tempContact); // where getEntry returns a list of all matching contacts
}
2
  • Why should BST (assuming that stands for binary search tree) be an enum? Commented Jun 27, 2013 at 13:10
  • in this case i'm not trying to make it enum, im just trying to have an enumerated type that could reference a specific object as a field in the class. based on the fact that im calling modify() from a different class. I could have an int field in modify that is then run through a check to say if(1) toFindIn = suchandsuchtree; or i could take the enum and create a switch statement but that doesnt seem anymore logical to me, i have looked up the more advanced used of enums and i feel like there is something im missing Commented Jun 27, 2013 at 13:13

1 Answer 1

2

Enum could provide different implementation of its method. A good example would be Math operation:

enum Op {
    PLUS {
        int exec(int l, int r) { return l + r; }
    },
    MINUS {
        int exec(int l, int r) { return l - r; }
    };
    abstract int exec(int l, int r);
}

Then I could do Op.PLUS.exec(5, 7) to perform 5 plus 7

See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more detail on how to use enum.

In your case, I wouldn't use enum for something having loads of logic and state, but here is how you could use enum with methods having different implementations.

enum BSTType {
    SearchTreeFirstName {
        void someMethod(Contact c) {...}
    },
    SearchTreeLastName {
        void someMethod(Contact c) {...}
    };
    abstract void somemethod(Contact c);
}

public boolean modify(Contact contactToFind, BSTType bstType, String newContactInfo) {
    // ...
    bstType.someMethod(contact);
    // ...
}

By looking at the variable name and class name, I think what you actually meant is indexing Contact in a TreeSet either by first name or last name

enum IndexType implements Comparator<Contact> {
    IndexByFirstName {
        @Override
        public int compare(Contact o1, Contact o2) {
            return o1.firstName.compareTo(o2.firstName);
        }
    },
    IndexByLastName {
        @Override
        public int compare(Contact o1, Contact o2) {
            return o1.lastName.compareTo(o2.lastName);
        }
    };
}
TreeSet<Contact> contacts = new TreeSet<Contact>(IndexType.IndexByLastName);
Sign up to request clarification or add additional context in comments.

3 Comments

Some explanation would be helpful. Please see How to Answer ?
In addition, you may not want the TreeSet to overwrite Contact with the same last name, you may find guava's Ordering.compound useful for creating comparator first ordered by lastname, then by an unique id
Your answer was very helpful, I tried to have exec method return a reference to the tree but ran into static problems which i expected, so i guess for now I will use a switch statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.