Results 1 to 17 of 17
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Creating an enum with String and Method

    How would I go about and make an enum, that has Strings and Methods? Can't find much help online

    EDIT: Hmm, this is too vague, I can't explain too well, I want to make a class called GraphicalEffects, this class and be instanstiated and it has a method to apply graphicalEffects AS methods or some type of references to methods in an ArrayList. This class should have an enum with methods to do this.

    Regards
    augustas656
    Last edited by augustas656; 11-09-2014 at 07:39 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating an enum with String and Method

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    Yes, but I'm still confused, I try to follow the examples, but I can't get anywhere, I've seen different ways of using enums and I don't understand them but they SEEM to be going the way of what I'm trying to accomplish.

    Regards
    augustas656

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating an enum with String and Method

    Perhaps if you posted what it is you have tried we can help. It may be that enums are not what you want. It sounds like you might want an Array of some interface implementation which can have one or more methods that can be invoked as the array is being iterated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    Well, right now I have public static final int with name of effect and then it equals to a number. Someone told me earlier it's better to use enums in this situation, but I'm trying to find a way to make the most compact and efficient code, I can make it work this simple way. I have a class for Color, and this class has methods to change hue, saturation, brightness, red, blue, green, grayness (practically negative brightness with negative saturation) etc of instances. My aim is to make another class which has an arraylist of references to methods in an enum and the class has a method to apply these methods referenced in the arraylist in order, to every pixel's color of BufferedImage passed in as the argument of the method. I don't even fully understand myself how the code is going to look or work, but I'm not sure where to look.

    Regards
    augustas656
    Regards
    augustas656

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating an enum with String and Method

    Here is something I threw together to demonstrate what they can do.

    Java Code:
    public class EnumExample {
       public static void main(String[] args) {
          MyEnum foo;
          foo = MyEnum.Name1;
          foo.MyMethod();
          MyEnum.Name2.MyMethod();
          MyEnum.Name3.MyMethod();
          System.out.println(foo.name() + "  " + foo.ordinal() + "  " + foo.getValue() + "  " + foo.getStr());
          
       }
    }
    
    enum MyEnum {
       Name1("One", 1) {
          public void MyMethod() {
             System.out.println("Doing something special for Name1");
          }
       }, Name2("Two", 2) {
          public void MyMethod() {
             System.out.println("Doing something special for Name2");
          }
       }, Name3("Three", 3);
       private String str;
       private int val;
      
       // override this for each enum value (not mandatory)
       public void MyMethod() {
          System.out.println("All the other values");
       }
       MyEnum(String str, int val) {
          this.str = str;
          this.val = val;
       }
       
       public String getStr() {
          return str;
       }
       public int getValue() {
          return val;
       }
       
       // other methods here
       
    }

    Also, the reason these are better than final static constants is that you don't have to worry about someone passing a "bad"
    value to a routine that expects a constant. So invariant checking of parameters is eliminated. Just have the enum as the parameter type to the method. For example.

    Java Code:
    public void someMethod(MyEnum foo) {
       // do something important
    }
    Now you can't pass anything but a legitimate value of MyEnum to someMethod or the compiler will complain.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    Ok, I've managed to get an example of the old way of doing what I'm trying to do:

    Java Code:
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Effects {
    	public static void Main(String[] args) {
    		Effects effectCluster = new Effects();
    		effectCluster.addEffect(Effects.EFFECT_1, 50);
    		effectCluster.addEffect(Effects.EFFECT_2, 11);
    		effectCluster.addEffect(Effects.EFFECT_2, 1);
    		effectCluster.addEffect(Effects.EFFECT_1, 75);
    		effectCluster.addEffect(Effects.EFFECT_3, 23);
    		effectCluster.addEffect(Effects.EFFECT_1, 24);
    		effectCluster.addEffect(Effects.EFFECT_3, 100);
    		
    		BufferedImage image1 = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    		BufferedImage image2 = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    		BufferedImage image3 = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    		
    		image1 = effectCluster.applyEffects(image1);
    		image2 = effectCluster.applyEffects(image2);
    		image3 = effectCluster.applyEffects(image3);
    	}
    	
    	public ArrayList<Effect> effects = new ArrayList<Effect>();
    	
    	class Effect {
    		public int type;
    		public int value;
    		
    		public Effect(int type, int value) {
    			this.type = type;
    			this.value = value;
    		}
    	}
    	
    	public static final int EFFECT_1 = 1;
    	public static final int EFFECT_2 = 2;
    	public static final int EFFECT_3 = 3;
    	
    	public void addEffect(int type, int value) {
    		effects.add(new Effect(type, value));
    	}
    	
    	public void removeEffect(int index) {
    		effects.remove(index);
    	}
    	
    	public void clearEffects() {
    		effects.clear();
    	}
    	
    	public BufferedImage applyEffects(BufferedImage image) {
    		for (Effect effect : effects) {
    			switch (effect.type) {
    			case EFFECT_1: image = effect1(image, effect.value); break;
    			case EFFECT_2: image = effect2(image, effect.value); break;
    			case EFFECT_3: image = effect3(image, effect.value); break;
    			}
    		} return image;
    	}
    	
    	public BufferedImage effect1(BufferedImage image, int intensity) {
    		BufferedImage affectedImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    		//Code to apply effect1;
    		return affectedImage;
    	}
    	
    	public BufferedImage effect2(BufferedImage image, int intensity) {
    		BufferedImage affectedImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    		//Code to apply effect1;
    		return affectedImage;
    	}
    	
    	public BufferedImage effect3(BufferedImage image, int intensity) {
    		BufferedImage affectedImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    		//Code to apply effect1;
    		return affectedImage;
    	}
    }
    Someone suggested that I should involve enums in this to make it better. And ofcourse, this is an example, I will have like 50 effects or more.

    Regards
    Augustas656
    Last edited by augustas656; 11-10-2014 at 09:23 PM.
    Regards
    augustas656

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating an enum with String and Method

    I am not certain what else I can provide. it should be straightforward to use my example and the tutorials to adapt your static fields to enums. Just remember that the method signature must have the enum type that you create (analogous to passing a class or interface type).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    Hmm, I mean I could easily manage to create an enum with just EFFECT variable names and integers, but I'm not sure how I would define methods in an enum beucase the value, intensnity of the effect is a choice, a parameter, not a constant and different effects have different ranges of intensnity. That are storable in reference form in an ArrayList. Unless... hmm, I'm gonna try something

    Regards
    augustas656
    Last edited by augustas656; 11-11-2014 at 12:07 AM.
    Regards
    augustas656

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating an enum with String and Method

    So, you pass it in, just as you're doing now.
    Your method signature would be something like:
    Java Code:
    public BufferedImage produceEffect(BufferedImage image, int intensity)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    What I'm trying to do is like eliminate the need for the switch and case, make my code more compact in addition to make it more efficient in enum form.
    Regards
    augustas656

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating an enum with String and Method

    If you want to eliminate your switch statement then create a class which takes the enum arguments and any others you need as constructors. Then simply instantiate them and place them in a List<>. You can then invoke the proper method by index selection. In fact, you could probably use the Lambda and method reference capabilities of Java 8 to help with this. But that will take a lot of reading and practice on your part.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating an enum with String and Method

    If the type attribute on the Effect class is an EffectEnum then your switch becomes:
    Java Code:
    effect.type.produceEffect(image, effect.value);
    In fact, since effect holds the value then maybe:
    Java Code:
    effect.produceEffect(image);
    which would then itself call the method on the EffectEnum.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    I don't exactly know what you meant by creating a class, but if the Lambda and method reference capabilties of Java 8 are documented as a tutorial in detail and you'd recommend me, then if its more efficient, then please direct me to some page. Because, I'm a bad searcher for good tutorials.

    EDIT: Wait... I read something, does java 8 let you, reference methods as parameters!? Or am I misunderstanding again??
    Regards
    augustas656

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating an enum with String and Method

    Lambda's are not more efficient since they tend to generate more byte code than what would normally be required, sans Lambda expressions. But they can reduce the amount of code to write to do certain things. And some claim they make the code easier to read. And yes, Java 8 does allow method references. But the effects are similar to what I was saying about interfaces. I really would recommend against lambdas at this point. Imho, you are already taking on more than you probably should. Learning to program takes a long time. Even if one knows other programming languages, that doesn't mean they know the Java capabilities. An oboist could not be expected to suddenly play a concerto on the piano. What you are trying to do, learn a language and architect an application is extremely hard (the architect part is difficult for experienced software architects). I would recommend you learn one thing (like enums) and practice using them, then go on to the next. I prefer written material over videos and there are lots of stuff on Java enums that cite examples. In any event, the forum will still be here to answer your questions.

    One of my favorite books is Effective Java (Second Edition) by Joshua Bloch ( ISBN-13: 978-0321356680). It does not teach Java. It is a best practices book. It explains what you should do, not do, and why. It covers up to Java 6.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Creating an enum with String and Method

    I'm a terrible textual learner... P:, I mostly rely on guided audiovisual tutorials, and so far all I know of java comes from that, except a few pieces that come from some forums and java documentation docs. I only ask on forums when I can't find anything on google. Exactly why some things are more efficient than others isn't very important to me, eventhough I do a lot of testing in a seperate project, I have a specific project that I'm building upon VERY slowly, and re-writting the code to a more efficient form. I really am just interested if something IS and ISNT efficient in most cases, but not why, not deeper down. I mean if you could link me to online sources, which some of might have tutorials on youtube based on the title of the tutorial if it's textual, I'll determine the dosage of knowledge income about java, and just incase I can easily store the sources to review later in any case.

    Not based on how much I should take in today, what would you recommend for me to use, Lambda's or enums, in my case, of the code I wrote earlier of the public static final constants I used. Recommend based on most compact and efficient form of code.
    Regards
    augustas656

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating an enum with String and Method

    I would say this is an enum thing, as each enum handles the application of an effect differently.
    But that's just me.

    Write an enum for your Effects similar to the structure given by jim above, but instead of myMethod give it a processEffect method with the parameters needed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 15
    Last Post: 06-18-2014, 06:51 PM
  2. Replies: 4
    Last Post: 04-02-2012, 02:50 PM
  3. Replies: 2
    Last Post: 03-23-2012, 05:53 AM
  4. Creating axis2 webservice-Enum type problem
    By sertacyilmaz in forum Advanced Java
    Replies: 1
    Last Post: 09-13-2011, 11:51 AM
  5. Replies: 4
    Last Post: 08-28-2009, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •