2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.validation
;
14 import javax
.validation
.ConstraintViolation
;
15 import javax
.validation
.Payload
;
18 * A class conveying the severity of a {@link ConstraintViolation}. Severity levels are
19 * extraneous to the javax.validation framework and can only be conveyed using generic
20 * {@link Payload} objects. Unfortunately, only the class of those objects is communicated
21 * back to the client. The class <i>is</i> the message. Concrete instances or {@code enum}
22 * s (an obvious choice for severity levels) cannot function as {@code Payload} objects.
23 * The Severity class enables you to program using true Severity instances (one for each
24 * level), while behind the scenes only the class of those instances is taken into
27 * @author ayco holleman
29 public abstract class Severity
implements Payload
{
31 public static final Notice NOTICE
= new Notice();
32 public static final Warning WARNING
= new Warning();
33 public static final Error ERROR
= new Error();
36 public static final class Notice
extends Severity
{};
37 public static final class Warning
extends Severity
{};
38 public static final class Error
extends Severity
{};
42 * Get {@code Severity} object for the specified {@code String} representation. Does the
43 * opposite of {@link #toString()}.
46 * The {@code String} representation of {@code Severity} object you want.
48 * @return The {@code Severity} object
50 public static Severity
forName(String name
)
52 if (name
.equals(Error
.class.getSimpleName())) {
55 if (name
.equals(Warning
.class.getSimpleName())) {
63 * Get the {@code Severity} of the specified {@code ConstraintViolation}.
66 * The {@code ConstraintViolation}
68 * @return The {@code Severity}
70 public static Severity
getSeverity(ConstraintViolation
<?
> error
)
72 Set
<Class
<?
extends Payload
>> payloads
= error
.getConstraintDescriptor().getPayload();
73 for (Class
<?
extends Payload
> payload
: payloads
) {
74 if (payload
== Error
.class) {
77 if (payload
== Warning
.class) {
80 if (payload
== Notice
.class) {
94 public String
toString()
96 return getClass().getSimpleName();