简体   繁体   中英

How to restrict Enum of certain Type?

How to restrict to use only one THUMNAIL ? Which means if a developer is using THUMNAIL_MID and THUMBNAIL, it should throw compile error so that the developer will know that only one THUMNAIL constant can be used.

Not Allowed

ImageSize[] imageArray = {ImageSize.A4,ImageSize.THUMBNAIL_MID,ImageSize.THUMBNAIL} ;

Allowed

ImageSize[] imageArray = {ImageSize.A4,ImageSize.THUMBNAIL_MID} ;

ENUM Code

    public enum ImageSize
    {
        THUMBNAIL(50, 50, "t"), PASSPORT(200, 200, "p"), SMALL(240, 135, "s"), MEDIUM(
                480, 270, "m"), LARGE(960, 540, "l"), A4(800, 600, "a4"),THUMBNAIL_MID(120,155,"t");
        /**
         * The width of the image in pixels
         */
        private final int width;
        /**
         * The height of the image in pixels
         */
        private final int height;
        /**
         * The image size type
         */
        private final String type;

        ImageSize(int width, int height, String type)
        {
            this.width = width;
            this.height = height;
            this.type = type;
        }

        public int getWidth()
        {
            return width;
        }

        public int getHeight()
        {
            return height;
        }

        public String getType()
        {
            return type;
        }

        public static ImageSize getImageSizeByType(String type)
        {
            if (type != null)
            {
                if (type.equalsIgnoreCase(THUMBNAIL.getType()))
                {
                    return THUMBNAIL;
                }
                if (type.equalsIgnoreCase(PASSPORT.getType()))
                {
                    return PASSPORT;
                }
                if (type.equalsIgnoreCase(SMALL.getType()))
                {
                    return SMALL;
                }
                if (type.equalsIgnoreCase(MEDIUM.getType()))
                {
                    return MEDIUM;
                }
                if (type.equalsIgnoreCase(LARGE.getType()))
                {
                    return LARGE;
                }
                if (type.equalsIgnoreCase(THUMBNAIL_MID.getType()))
                {
                    return THUMBNAIL_MID;
                }
            }
            return null;
        }
    }

}

I would recommend creating a new enum. The new enum can contain a single field - a reference to the old enum. You create values in the new enum corresponding to the values of the old enum you wish to allow.

Then in your code, you use the new enum type (where appropriate). I assume if it was always appropriate to use the new enum type you could have just deleted the extra enum types.

I used the name AllowableImageSize because I am not familiar with the context. You should probably pick a better name (eg, ThumbnailImageSize, PassportImageSize, LinkImageSize, etc.).

You are not restricted to creating one new enum. Depending on your needs, you could have a ThumbnailImageSize and PassportImageSize which point to different subsets of ImageSize.

I created sample code on ideone .

class Main
{
        public static void main ( String [ ] args )
        {
        }
}


enum AllowableImageSize
{
        THUMBNAIL_MID(ImageSize.THUMBNAIL_MID), THUMBNAIL(ImageSize.THUMBNAIL);

        public final ImageSize imageSize;

        AllowableImageSize(ImageSize imageSize)
        {
                this.imageSize=imageSize;
        }
}

    enum ImageSize
    {
        THUMBNAIL(50, 50, "t"), PASSPORT(200, 200, "p"), SMALL(240, 135, "s"), MEDIUM(
                480, 270, "m"), LARGE(960, 540, "l"), A4(800, 600, "a4"),THUMBNAIL_MID(120,155,"t");
        /**
         * The width of the image in pixels
         */
        private final int width;
        /**
         * The height of the image in pixels
         */
        private final int height;
        /**
         * The image size type
         */
        private final String type;

        ImageSize(int width, int height, String type)
        {
            this.width = width;
            this.height = height;
            this.type = type;
        }

        public int getWidth()
        {
            return width;
        }

        public int getHeight()
        {
            return height;
        }

        public String getType()
        {
            return type;
        }

        public static ImageSize getImageSizeByType(String type)
        {
            if (type != null)
            {
                if (type.equalsIgnoreCase(THUMBNAIL.getType()))
                {
                    return THUMBNAIL;
                }
                if (type.equalsIgnoreCase(PASSPORT.getType()))
                {
                    return PASSPORT;
                }
                if (type.equalsIgnoreCase(SMALL.getType()))
                {
                    return SMALL;
                }
                if (type.equalsIgnoreCase(MEDIUM.getType()))
                {
                    return MEDIUM;
                }
                if (type.equalsIgnoreCase(LARGE.getType()))
                {
                    return LARGE;
                }
                if (type.equalsIgnoreCase(THUMBNAIL_MID.getType()))
                {
                    return THUMBNAIL_MID;
                }
            }
            return null;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM