1
18 package org.torweg.pulse.component.statistics.model.aggregation;
19
20 import javax.persistence.Basic;
21 import javax.persistence.ManyToOne;
22 import javax.persistence.MappedSuperclass;
23 import javax.xml.bind.annotation.XmlAccessOrder;
24 import javax.xml.bind.annotation.XmlAccessType;
25 import javax.xml.bind.annotation.XmlAccessorOrder;
26 import javax.xml.bind.annotation.XmlAccessorType;
27 import javax.xml.bind.annotation.XmlElement;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import javax.xml.bind.annotation.XmlTransient;
30
31 import org.hibernate.LazyInitializationException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.torweg.pulse.component.statistics.model.RegexVersion;
35 import org.torweg.pulse.util.entity.AbstractBasicEntity;
36
37
51 @XmlRootElement(name = "abstract-regex-versioned-counter")
52 @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
53 @XmlAccessorType(XmlAccessType.FIELD)
54 @MappedSuperclass
55 public abstract class AbstractRegexVersionedCounter<T extends AbstractAggregation>
56 extends AbstractBasicEntity implements ICount {
57
58
61 private static final long serialVersionUID = 172778008120627703L;
62
63
66 private static final Logger LOGGER = LoggerFactory
67 .getLogger(AbstractRegexVersionedCounter.class);
68
69
73 @ManyToOne(optional = false)
74 @XmlTransient
75 private T abstractAggregation;
77
78
81 @ManyToOne(optional = false)
82 @XmlTransient
83 private RegexVersion version;
85
86
89 @Basic
90 @XmlElement(name = "counter")
91 private int counter;
92
93
99 @XmlTransient
100 public final T getAggreation() {
101 return this.abstractAggregation;
102 }
103
104
109 @XmlElement(name = "aggregation-id")
110 @SuppressWarnings("unused")
111 @Deprecated
112 private Long getAggreationIdJAXB() {
113 try {
114 return ((AbstractBasicEntity) getAggreation()).getId();
115 } catch (LazyInitializationException e) {
116 LOGGER.debug("igonred: " + e.getLocalizedMessage(), e);
117 return null;
118 }
119 }
120
121
134 protected final void setAggreation(final T aggregation) {
135 if (aggregation == null) {
136 throw new NullPointerException(
137 "Given AbstractAggregation must not be null");
138 }
139 this.abstractAggregation = aggregation;
140 }
141
142
147 public final int getCount() {
148 return this.counter;
149 }
150
151
163 protected final void setCounter(final int count) {
164 if (count < 0) {
165 throw new IllegalArgumentException("Given count must not be < 0.");
166 }
167 this.counter = count;
168 }
169
170
175 @XmlTransient
176 public final RegexVersion getVersion() {
177 return this.version;
178 }
179
180
185 @XmlElement(name = "version")
186 @SuppressWarnings("unused")
187 @Deprecated
188 private RegexVersion getVersionJAXB() { try {
190 return getVersion();
191 } catch (LazyInitializationException e) {
192 LOGGER.debug("ignored: {}", e.getLocalizedMessage());
193 return null;
194 }
195 }
196
197
209 protected final void setVersion(final RegexVersion v) {
210 if (v == null) {
211 throw new NullPointerException("Given Version must not be null");
212 }
213 this.version = v;
214 }
215
216
219 public final void increase() {
220 this.counter++;
221 }
222
223
232 public final boolean isMatch(final String s) {
233 return s.matches(getVersion().getRegex());
234 }
235
236
242 @Override
243 public String toString() {
244 return "{" + super.toString() + "@[" + getId() + "], count: "
245 + getCount() + ", aggr: "
246 + getAggreation().getClass().getCanonicalName() + ", version: "
247 + getVersion() + "}";
248 }
249
250 }
251