Skip to content

Better Java, IntelliJ IDEA backed, primitive type enums and flags

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
license.template
Notifications You must be signed in to change notification settings

cheblin/SlimEnum

Repository files navigation

SlimEnum

Well known, that in some use cases Java enums are not so good as they could be.

The drawback of Java enums is especially obvious when they are heavy used in high loaded systems, whenever performance and memory consumptions are crucial.

To overcome this problem developers use simple, primitive type constants instead of enums.

The advantage of this solution is its lightweight, natural, fit to build as simple enums, as flags.

But this has some limitation: no compile-time check and no IDEA code completion support.

The root of the problem is that the Java type system does not support the building of new types by extending primitives.

It is possible to utilize java annotation to declare the named constants set and bind it to primitive type variables, fields, methods arguments and return type.

With this plugin, IDE gets smart code completion capability.

SlimEnum is alternative approach, especially good in the case when performance is a concern and primitive constants can be better used.
SlimEnum is not a total replacement for Java language embedded enums, of course.
SlimEnum is similar to but not the same as IntDef in Android.

Step-by-step guide

Declare SlimEnum

@interface Font { //declare Font annotation with constant fields
	
	byte    NORMAL    = 0, //constants set for variables of byte type
                BOLD      = 1,
                UNDERLINE = 3,
                BLINK     = 4,
                INVERSE   = 5,
                STRIKE    = 6;
	
	String  Helvetica  = "Helvetica", //constants set if variable is String
                Palatino   = "Palatino",
                HonMincho  = "HonMincho",
                Serif      = "Serif",
                Monospaced = "Monospaced",
                Dialog     = "Dialog";
	
	@interface Foreground { // enclosed declarations is ok
		int     BLACK   = 0, // if the first and
                        RED     = 1, // second constant names follow in alphabetic order this is just enum
                        GREEN   = 2, // a constant value for field can be used only once, values combinations have no sense
                        YELLOW  = 3,
                        BLUE    = 4,
                        MAGENTA = 5,
                        CYAN    = 6,
                        WHITE   = 7,
                        DEFAULT = 8;
		
		class Validator { //put some related code
			public static boolean ok( int value ) {
				switch (value)
				{
					case BLACK:
					case RED:
					case GREEN:
					case YELLOW:
					case BLUE:
					case MAGENTA:
					case CYAN:
					case WHITE:
					case DEFAULT:
						return true;
				}
				return false;
			}
		}

	}
	
	@interface Background {
		int     RED         = 1, // if the first and
                        BLACK       = 0, // second constant names follow in descending alphabetical order means this is a flag
                        GREEN       = 2, // a constant value combination for field is ok
                        YELLOW      = 3,
                        BLUE        = 4,
                        MAGENTA     = 5,
                        CYAN        = 6,
                        WHITE       = 7,
                        DEFAULT     = 8,
                        TRANSPARENT = 0;
	}
}

apply to variables, fields, methods arguments and return type

class Test {
	@Font String name;
	@Font.Foreground int fg;
	
	@Font.Background int setBackground( @Font.Background int bg ) { return bg; }
	
	static void createFont( @Font String name, @Font byte style, @Font.Background int background, @Font.Foreground int foregraund ) { }
}

...and then use it

public class Main {
	
	public static void main( String[] args )  {
		Test.createFont( Font.Monospaced + Font.HonMincho, Font.BLINK, Font.Background.CYAN, Font.Foreground.BLACK );
		@Font String         name = Font.Helvetica;
		@Font byte           type = Font.NORMAL | Font.BOLD | Font.INVERSE;
		@Font.Foreground int fg   = Font.Foreground.BLUE;
		
		Test test = new Test();
		test.setBackground( Font.Background.BLUE );
		test.fg = Font.Foreground.BLUE | Font.Foreground.CYAN | Font.Foreground.MAGENTA;
		
		test.fg = Font.Foreground.BLUE;
		
		@Font byte[]         array = new byte[10];
		
		array[2] = Font.BLINK | Font.BOLD;
		
		if (type == (Font.NORMAL | Font.BOLD | Font.INVERSE | Font.BLINK | Font.STRIKE) && test.setBackground( Font.Background.BLUE ) == Font.Background.RED)
		{
			@Font.Foreground int text_color = Font.Foreground.CYAN;
		}
		assert (test.setBackground( Font.Background.BLUE ) == (Font.Background.CYAN | Font.Background.DEFAULT));
		
		if (test.fg == Font.NORMAL && test.fg != Font.NORMAL)
		{
		}
		
		@Font String fontName = Font.Dialog;
		
		int random_value = 99;
		
		if (Font.Foreground.Validator.ok( random_value ))
			switch (test.setBackground( Font.Background.BLUE ))
			{
				case Font.Background.BLUE:
					break;
				case Font.Background.CYAN:
					break;
			}
	}
}

1

3

4

5

6

6

Installation

  • Using IDE built-in plugin system:

    Settings/Preferences > Plugins > Marketplace > Search for "SlimEnum" > Install Plugin

  • Manually:

    Download the latest release and install it manually using Settings/Preferences > Plugins > ⚙️ > Install plugin from disk...


Plugin based on the IntelliJ Platform Plugin Template.

About

Better Java, IntelliJ IDEA backed, primitive type enums and flags

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
license.template

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages