View Javadoc

1   package org.csc.phynixx.loggersystem.logrecord;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  /**
25   * A LogRecord is an atomic piece of information.  This piece of Information ist qualified by an ordinal of an superior.
26   * <p/>
27   * The current class manages the content and is not responsible for writing/persisting the content
28   */
29  class PhynixxDataRecord implements IDataRecord, Comparable<PhynixxDataRecord> {
30  
31      private static final byte[][] EMPTY_DATA = new byte[][]{};
32  
33      private static final byte[] EMPTY_BYTES = new byte[]{};
34  
35      private Integer ordinal = null;
36  
37      /**
38       * backward reference to the owner of the log record
39       */
40      private long messageSequenceId;
41  
42      /**
43       * content
44       */
45      private byte[][] records = null;
46  
47      private XALogRecordType logRecordType = XALogRecordType.UNKNOWN;
48  
49  
50      /**
51       * If
52       *
53       * @param messageSequenceId
54       * @param ordinal
55       * @param logRecordType
56       * @param data
57       */
58      PhynixxDataRecord(long messageSequenceId, int ordinal, XALogRecordType logRecordType, byte[][] data) {
59  
60          this.ordinal = ordinal;
61          this.logRecordType = logRecordType;
62          this.messageSequenceId = messageSequenceId;
63          this.setRecords(data);
64      }
65  
66  
67      public XALogRecordType getLogRecordType() {
68          return logRecordType;
69      }
70  
71      public long getXADataRecorderId() {
72          return this.messageSequenceId;
73      }
74  
75      public byte[][] getData() {
76          return (records != null) ? records : EMPTY_DATA;
77      }
78  
79  
80      /**
81       * replaces null with EMPTY_DATA
82       *
83       * @param data
84       */
85      private void setRecords(byte[][] data) {
86          if (data == null || data.length==0) {
87              this.records = EMPTY_DATA;
88              return;
89          }
90  
91          this.records = new byte[data.length][];
92          for (int i = 0; i < data.length; i++) {
93              if (data[i] != null) {
94                  this.records[i] = data[i];
95              } else {
96                  this.records[i] = EMPTY_BYTES;
97              }
98          }
99      }
100 
101     /**
102      * a log record is part of a record sequence and it knows its ordinal
103      *
104      * @return
105      */
106     public Integer getOrdinal() {
107         return this.ordinal;
108     }
109 
110 
111     public String toString() {
112         StringBuffer recordLength = new StringBuffer();
113 
114         recordLength.append("{");
115         if (this.records != null && this.records.length > 0) {
116             for (int i = 0; i < records.length; i++) {
117                 recordLength.append('[').append(records[i].length).append("] ");
118             }
119         }
120         recordLength.append("}");
121         return "LogMessage messageSequence=" + this.getXADataRecorderId() + " ordinal=" + this.getOrdinal() + " logRecordType=" + logRecordType + " records=" + recordLength.toString();
122     }
123 
124     public int hashCode() {
125         final int prime = 31;
126         int result = 1;
127         result = prime * result + Long.valueOf(messageSequenceId).intValue();
128         result = prime * result + ((ordinal == null) ? 0 : ordinal.hashCode());
129         return result;
130     }
131 
132     public boolean equals(Object obj) {
133         if (this == obj)
134             return true;
135         if (obj == null)
136             return false;
137         if (getClass() != obj.getClass())
138             return false;
139 
140         if (obj instanceof PhynixxDataRecord) {
141             return this.compareTo((PhynixxDataRecord) obj) == 0;
142         } else {
143             return false;
144         }
145     }
146 
147     public int compareTo(PhynixxDataRecord otherMsg) {
148         if (otherMsg == null) {
149             return 1;
150         }
151 
152         int cmp = Long.valueOf(this.getXADataRecorderId() - otherMsg.getXADataRecorderId()).intValue();
153         if (cmp != 0) {
154             return cmp;
155         } else {
156             int diff = this.getOrdinal().intValue() - otherMsg.getOrdinal().intValue();
157             return (diff == 0) ? 0 : (diff < 0) ? -1 : 1;
158         }
159 
160     }
161 
162 
163 }