3acb23041c14dce9bcc1caca712481ac1081cc34
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / MustDefinitionImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
13 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
14
15 /**
16  * Immutable implementation of {@link MustDefinition}
17  */
18 public final class MustDefinitionImpl implements MustDefinition {
19     private final String mustStr;
20     private final String description;
21     private final String reference;
22     private final String errorAppTag;
23     private final String errorMessage;
24
25     /**
26      * Creates new Must Definition
27      *
28      * @param mustStr must string statement, Must not be null.
29      * @param description Description of condition
30      * @param reference Reference for condition
31      * @param errorAppTag error application tag which should be used for error reporting when condition fails
32      * @param errorMessage message  which should be used for error reporting when condition fails
33      */
34     private MustDefinitionImpl(final String mustStr, final String description, final String reference, final String errorAppTag, final String errorMessage) {
35         this.mustStr = Preconditions.checkNotNull(mustStr);
36         this.description = description;
37         this.reference = reference;
38         this.errorAppTag = errorAppTag;
39         this.errorMessage = errorMessage;
40     }
41
42     /**
43     *
44     * Creates new Must Definition
45     *
46     * @param mustStr must string statement, Must not be null.
47     * @param description Description of condition
48     * @param reference Reference for condition
49     * @param errorAppTag error application tag which should be used for error reporting when condition fails
50     * @param errorMessage message  which should be used for error reporting when condition fails
51     */
52     public static MustDefinitionImpl create(final String mustStr, final Optional<String> description,
53             final Optional<String> reference, final Optional<String> errorAppTag, final Optional<String> errorMessage) {
54         return new MustDefinitionImpl(mustStr, description.orNull(), reference.orNull(), errorAppTag.orNull(), errorMessage.orNull());
55     }
56
57     @Override
58     public String getDescription() {
59         return description;
60     }
61
62     @Override
63     public String getErrorAppTag() {
64         return errorAppTag;
65     }
66
67     @Override
68     public String getErrorMessage() {
69         return errorMessage;
70     }
71
72     @Override
73     public String getReference() {
74         return reference;
75     }
76
77     @Override
78     public RevisionAwareXPath getXpath() {
79         return null;
80     }
81
82     @Override
83     public int hashCode() {
84         final int prime = 31;
85         int result = 1;
86         result = prime * result + ((mustStr == null) ? 0 : mustStr.hashCode());
87         result = prime * result + ((description == null) ? 0 : description.hashCode());
88         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
89         return result;
90     }
91
92     @Override
93     public boolean equals(final Object obj) {
94         if (this == obj) {
95             return true;
96         }
97         if (obj == null) {
98             return false;
99         }
100         if (getClass() != obj.getClass()) {
101             return false;
102         }
103         final MustDefinitionImpl other = (MustDefinitionImpl) obj;
104         if (mustStr == null) {
105             if (other.mustStr != null) {
106                 return false;
107             }
108         } else if (!mustStr.equals(other.mustStr)) {
109             return false;
110         }
111         if (description == null) {
112             if (other.description != null) {
113                 return false;
114             }
115         } else if (!description.equals(other.description)) {
116             return false;
117         }
118         if (reference == null) {
119             if (other.reference != null) {
120                 return false;
121             }
122         } else if (!reference.equals(other.reference)) {
123             return false;
124         }
125         return true;
126     }
127
128     @Override
129     public String toString() {
130         return mustStr;
131     }
132
133 }