java - 如何限制某种类型的Enum?

标签 java jakarta-ee

如何限制只使用一个THUMNAIL?这意味着如果开发人员使用 THUMNAIL_MID 和 THUMBNAIL,它应该抛出编译错误,以便开发人员知道只能使用一个 THUMNAIL 常量。

不允许

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

允许

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

枚举代码

    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;
        }
    }

}

最佳答案

我建议创建一个新的枚举。新枚举可以包含单个字段 - 对旧枚举的引用。您在新枚举中创建与您希望允许的旧枚举值相对应的值。

然后在代码中使用新的枚举类型(在适当的情况下)。我认为如果使用新的枚举类型总是合适的,您可以删除额外的枚举类型。

我使用了AllowableImageSize 这个名称,因为我不熟悉上下文。您可能应该选择一个更好的名称(例如,ThumbnailImageSize、PassportImageSize、LinkImageSize 等)。

您不限于创建一个新的枚举。根据您的需要,您可以使用 ThumbnailImageSize 和 PassportImageSize 来指向 ImageSize 的不同子集。

我在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;
        }
    }

关于java - 如何限制某种类型的Enum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751318/

相关文章:

java - 使用 Java Servlet 读取和写入以太网端口的替代方案

java - PowerMockito 模拟单个静态方法并返回对象

java - 如何根据请求有效负载将请求与 Java EE 应用程序中的 HttpSession 相关联?

jakarta-ee - 使用域名访问Web应用程序

java - 第一个 Java EE Spring 项目的 "best practice"是什么?

java - 需要根据 Servlet 响应有条件地设置 HTTP header

java - 查找项目源文件夹中特定扩展名的所有文件

java - 使用 JScrollBar 更新组件

java - 如何遍历字符串来查找特定字符?

java - 如何将多个参数从 View 传递到 Controller 的方法