Merge "BUG-582: pre-cache Pattern instances"
[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 org.opendaylight.yangtools.yang.model.api.MustDefinition;
11 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15
16 /**
17  *
18  * Immutable implementation of {@link MustDefinition}
19  *
20  */
21 public final class MustDefinitionImpl implements MustDefinition {
22     private final String mustStr;
23     private final String description;
24     private final String reference;
25     private final String errorAppTag;
26     private final String errorMessage;
27
28     /**
29      *
30      * Creates new Must Definition
31      *
32      * @param mustStr must string statement, Must not be null.
33      * @param description Description of condition
34      * @param reference Reference for condition
35      * @param errorAppTag error application tag which should be used for error reporting when condition fails
36      * @param errorMessage message  which should be used for error reporting when condition fails
37      * @deprecated Use {@link #create(String, Optional, Optional, Optional, Optional)} instead.
38      */
39     @Deprecated
40     public MustDefinitionImpl(final String mustStr, final String description, final String reference, final String errorAppTag, final String errorMessage) {
41         this.mustStr = Preconditions.checkNotNull(mustStr);
42         this.description = description;
43         this.reference = reference;
44         this.errorAppTag = errorAppTag;
45         this.errorMessage = errorMessage;
46     }
47
48     /**
49     *
50     * Creates new Must Definition
51     *
52     * @param mustStr must string statement, Must not be null.
53     * @param description Description of condition
54     * @param reference Reference for condition
55     * @param errorAppTag error application tag which should be used for error reporting when condition fails
56     * @param errorMessage message  which should be used for error reporting when condition fails
57     */
58     public static MustDefinitionImpl create(final String mustStr, final Optional<String> description, final Optional<String> reference, final Optional<String> errorAppTag, final Optional<String> errorMessage) {
59         return new MustDefinitionImpl(mustStr, description.orNull(), reference.orNull(), errorAppTag.orNull(), errorMessage.orNull());
60     }
61
62     @Override
63     public String getDescription() {
64         return description;
65     }
66
67     @Override
68     public String getErrorAppTag() {
69         return errorAppTag;
70     }
71
72     @Override
73     public String getErrorMessage() {
74         return errorMessage;
75     }
76
77     @Override
78     public String getReference() {
79         return reference;
80     }
81
82     @Override
83     public RevisionAwareXPath getXpath() {
84         return null;
85     }
86
87     @Override
88     public int hashCode() {
89         final int prime = 31;
90         int result = 1;
91         result = prime * result + ((mustStr == null) ? 0 : mustStr.hashCode());
92         result = prime * result + ((description == null) ? 0 : description.hashCode());
93         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
94         return result;
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (obj == null) {
103             return false;
104         }
105         if (getClass() != obj.getClass()) {
106             return false;
107         }
108         final MustDefinitionImpl other = (MustDefinitionImpl) obj;
109         if (mustStr == null) {
110             if (other.mustStr != null) {
111                 return false;
112             }
113         } else if (!mustStr.equals(other.mustStr)) {
114             return false;
115         }
116         if (description == null) {
117             if (other.description != null) {
118                 return false;
119             }
120         } else if (!description.equals(other.description)) {
121             return false;
122         }
123         if (reference == null) {
124             if (other.reference != null) {
125                 return false;
126             }
127         } else if (!reference.equals(other.reference)) {
128             return false;
129         }
130         return true;
131     }
132
133     @Override
134     public String toString() {
135         return mustStr;
136     }
137
138 }