Merge "Use Optional to avoid null return value"
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / type / MappingData.java
1 /*
2  * Copyright (c) 2016, 2017 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
9 package org.opendaylight.lispflowmapping.lisp.type;
10
11 import com.google.common.base.Optional;
12 import java.util.Date;
13 import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
16
17 /**
18  * Wrapper class for MappingRecord with timestamp added.
19  *
20  * @author Lorand Jakab
21  *
22  */
23 public class MappingData {
24     private MappingRecord record = null;
25     private Date timestamp = null;
26     private XtrId xtrId = null;
27     private boolean mergeEnabled = false;
28
29     public MappingData(MappingRecord record, Long timestamp) {
30         this(record, new Date(timestamp));
31     }
32
33     public MappingData(MappingRecord record, Date timestamp) {
34         setRecord(record);
35         setTimestamp(timestamp);
36     }
37
38     public MappingData(MappingRecord record) {
39         setRecord(record);
40     }
41
42     public synchronized MappingRecord getRecord() {
43         return record;
44     }
45
46     public synchronized void setRecord(MappingRecord record) {
47         this.record = record;
48     }
49
50     public synchronized Date getTimestamp() {
51         return timestamp;
52     }
53
54     public synchronized void setTimestamp(Date timestamp) {
55         this.timestamp = timestamp;
56     }
57
58     public synchronized XtrId getXtrId() {
59         return xtrId;
60     }
61
62     public synchronized void setXtrId(XtrId xtrId) {
63         this.xtrId = xtrId;
64     }
65
66     public synchronized boolean isMergeEnabled() {
67         return mergeEnabled;
68     }
69
70     public synchronized void setMergeEnabled(boolean mergeEnabled) {
71         this.mergeEnabled = mergeEnabled;
72     }
73
74     public Optional<Boolean> isNegative() {
75         if (record != null) {
76             return Optional.of(MappingRecordUtil.isNegativeMapping(record));
77         } else {
78             return Optional.absent();
79         }
80     }
81
82     public Optional<Boolean> isPositive() {
83         if (record != null) {
84             return Optional.of(MappingRecordUtil.isPositiveMapping(record));
85         } else {
86             return Optional.absent();
87         }
88     }
89
90     @Override
91     public String toString() {
92         StringBuilder sb = new StringBuilder("MappingData [");
93
94         sb.append("merge=");
95         sb.append(mergeEnabled);
96
97         if (xtrId != null) {
98             sb.append(", xTR-ID=");
99             sb.append(xtrId);
100         }
101
102         if (timestamp != null) {
103             sb.append(", timestamp=");
104             sb.append(timestamp);
105         }
106
107         if (record != null) {
108             sb.append(", record=");
109             sb.append(record);
110         }
111
112         return sb.append(']').toString();
113     }
114 }