PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src_back/rssi_graph/rssi/RSSI2DistInternalBuffer.java

https://github.com/ph4r05/WirelessSensorNodeGeolocation
Java | 432 lines | 243 code | 68 blank | 121 comment | 54 complexity | 2c1e5bf560864cb051d4632ca13146d6 MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package rssi_graph.rssi;
  6. import java.util.Collections;
  7. import java.util.Iterator;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. /**
  11. * Internal library independent data structure to hold RSSI measurements
  12. * with relation to distance or other parameters.
  13. *
  14. * Can be used to pass data to graph, output, another process for further processing
  15. * @author ph4r05
  16. */
  17. public class RSSI2DistInternalBuffer implements Cloneable, Comparable<RSSI2DistInternalBuffer> {
  18. /**
  19. * =========================================================================
  20. *
  21. * DETERMINANTS
  22. *
  23. * =========================================================================
  24. */
  25. private int testno=-1;
  26. private int mid=-1;
  27. private int txpower=-1;
  28. private int distance=-1;
  29. private int talkingMote=-1;
  30. private int reportingMote=-1;
  31. // xvalue for graph
  32. private double xvalue=-1;
  33. /**
  34. * =========================================================================
  35. *
  36. * DATA
  37. *
  38. * =========================================================================
  39. */
  40. /**
  41. * Raw data in linked list,should be unmodifiable
  42. */
  43. private List<RSSI2DistInternalBufferRaw> raw=null;
  44. /**
  45. * Statistical data (Descriptive Statistics)
  46. * Should be always consistent with raw data if not null
  47. * Should be read-only
  48. */
  49. private RSSI2DistInternalBufferStats stats=null;
  50. /**
  51. * Clone method, clone whole objects with data
  52. *
  53. * @return
  54. * @throws CloneNotSupportedException
  55. */
  56. @Override
  57. protected Object clone() throws CloneNotSupportedException {
  58. RSSI2DistInternalBuffer clone=(RSSI2DistInternalBuffer)super.clone();
  59. // copy attributes
  60. clone.setDistance(this.getDistance());
  61. clone.setMid(this.getMid());
  62. clone.setReportingMote(this.getReportingMote());
  63. clone.setTalkingMote(this.getTalkingMote());
  64. clone.setTestno(this.getTestno());
  65. clone.setTxpower(this.getTxpower());
  66. clone.setXvalue(this.getXvalue());
  67. // clone stat data
  68. clone.stats = stats!=null ? (RSSI2DistInternalBufferStats) this.getStats().clone() : null;
  69. // clone raw data
  70. if (raw != null && raw instanceof List){
  71. clone.raw = new LinkedList<RSSI2DistInternalBufferRaw>();
  72. Iterator<RSSI2DistInternalBufferRaw> it = raw.iterator();
  73. while(it.hasNext()){
  74. clone.raw.add((RSSI2DistInternalBufferRaw) it.next().clone());
  75. }
  76. }
  77. return clone;
  78. }
  79. @Override
  80. public boolean equals(Object obj) {
  81. if (obj == null) {
  82. return false;
  83. }
  84. if (getClass() != obj.getClass()) {
  85. return false;
  86. }
  87. final RSSI2DistInternalBuffer other = (RSSI2DistInternalBuffer) obj;
  88. if (this.testno != other.testno) {
  89. return false;
  90. }
  91. if (this.mid != other.mid) {
  92. return false;
  93. }
  94. if (this.txpower != other.txpower) {
  95. return false;
  96. }
  97. if (this.distance != other.distance) {
  98. return false;
  99. }
  100. if (this.talkingMote != other.talkingMote) {
  101. return false;
  102. }
  103. if (this.reportingMote != other.reportingMote) {
  104. return false;
  105. }
  106. if (Double.doubleToLongBits(this.xvalue) != Double.doubleToLongBits(other.xvalue)) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. @Override
  112. public int hashCode() {
  113. int hash = 7;
  114. hash = 67 * hash + this.testno;
  115. hash = 67 * hash + this.mid;
  116. hash = 67 * hash + this.txpower;
  117. hash = 67 * hash + this.distance;
  118. hash = 67 * hash + this.talkingMote;
  119. hash = 67 * hash + this.reportingMote;
  120. hash = 67 * hash + (int) (Double.doubleToLongBits(this.xvalue) ^ (Double.doubleToLongBits(this.xvalue) >>> 32));
  121. return hash;
  122. }
  123. /**
  124. * Basic compare method.
  125. * If another comparison criteria are needed, define own comparator.
  126. *
  127. * Default Compare priority:
  128. * - testno
  129. * - talking mote
  130. * - reporting mote
  131. * - txpower
  132. * - distance
  133. * - mid
  134. * @param o
  135. * @return
  136. */
  137. public int compareTo(RSSI2DistInternalBuffer other) {
  138. final int BEFORE = -1;
  139. final int EQUAL = 0;
  140. final int AFTER = 1;
  141. if (other == null) return -1;
  142. if (getClass() != other.getClass()) return -1;
  143. //this optimization is usually worthwhile, and can
  144. //always be added
  145. if ( this == other ) return EQUAL;
  146. if (this.testno < other.testno) return BEFORE;
  147. if (this.testno > other.testno) return AFTER;
  148. if (this.talkingMote < other.talkingMote) return BEFORE;
  149. if (this.talkingMote > other.talkingMote) return AFTER;
  150. if (this.reportingMote < other.reportingMote) return BEFORE;
  151. if (this.reportingMote > other.reportingMote) return AFTER;
  152. if (this.txpower < other.txpower) return BEFORE;
  153. if (this.txpower > other.txpower) return AFTER;
  154. if (this.distance < other.distance) return BEFORE;
  155. if (this.distance > other.distance) return AFTER;
  156. if (this.mid < other.mid) return BEFORE;
  157. if (this.mid > other.mid) return AFTER;
  158. //all comparisons have yielded equality
  159. //verify that compareTo is consistent with equals (optional)
  160. assert this.equals(other) : "compareTo inconsistent with equals.";
  161. return EQUAL;
  162. }
  163. /**
  164. * Override to string
  165. * Should return in same order as compareTo defines compare relation
  166. * @return
  167. */
  168. @Override
  169. public String toString() {
  170. StringBuilder sb = new StringBuilder();
  171. sb.append("RSSI2DistInternalBuffer[testno=").append(this.testno)
  172. .append("; talkingMote=").append(this.talkingMote)
  173. .append("; reportingMote=").append(this.reportingMote)
  174. .append("; txpower=").append(this.txpower)
  175. .append("; distance=").append(this.distance)
  176. .append("; mid=").append(this.mid)
  177. .append("; xvalue=").append(this.xvalue)
  178. .append("; rawData=").append(this.raw==null ? "null" : this.raw.size())
  179. .append("; stats=").append(this.stats==null ? "no" : "yes")
  180. .append("] ");
  181. return sb.toString();
  182. }
  183. /**
  184. * =========================================================================
  185. *
  186. * CONSTRUCTORS
  187. *
  188. * =========================================================================
  189. */
  190. public RSSI2DistInternalBuffer() {
  191. }
  192. public RSSI2DistInternalBuffer(int testno, int mid, int txpower, int distance, int talkingMote, int reportingMote) {
  193. this.testno = testno;
  194. this.mid = mid;
  195. this.txpower = txpower;
  196. this.distance = distance;
  197. this.talkingMote = talkingMote;
  198. this.reportingMote = reportingMote;
  199. }
  200. /**
  201. * Compute statistics for loaded raw data
  202. */
  203. public void computeStats(){
  204. if (this.raw==null){
  205. this.stats=null;
  206. return;
  207. }
  208. this.stats = RSSI2DistInternalBufferStats.getInstance(this.getRaw());
  209. }
  210. /**
  211. * delete data
  212. */
  213. public void clear(){
  214. this.setRaw(null);
  215. }
  216. /**
  217. * Load raw data from Integer array
  218. * Suitable for mass data load (from resultsets, measurement queues)
  219. * @param a
  220. */
  221. public void loadData(Integer[] a){
  222. if (a==null){
  223. throw new IllegalArgumentException("Null object passed");
  224. }
  225. this.clear();
  226. LinkedList<RSSI2DistInternalBufferRaw> tmpList = new LinkedList<RSSI2DistInternalBufferRaw>();
  227. for(int i=0, cn=a.length; i<cn; i++){
  228. tmpList.offer(new RSSI2DistInternalBufferRaw(a[i]));
  229. }
  230. this.raw = Collections.unmodifiableList(tmpList);
  231. this.stats = null;
  232. }
  233. /**
  234. * Load raw data from 2D integer array
  235. * [1d] = only sequence number
  236. * [2d] = only 1 element, rssi value
  237. *
  238. * Suitable when extracting raw data from resultset containing only RSSI values
  239. * @param a
  240. */
  241. public void loadData(Integer[][] a){
  242. if (a==null){
  243. throw new IllegalArgumentException("Null object passed");
  244. }
  245. this.clear();
  246. LinkedList<RSSI2DistInternalBufferRaw> tmpList = new LinkedList<RSSI2DistInternalBufferRaw>();
  247. for(int i=0, cn=a.length; i<cn; i++){
  248. if (a[i]==null || a[i].length<1 || a[i].length>1){
  249. throw new IllegalArgumentException("Subarray is illegal (null or does not have correct size");
  250. }
  251. tmpList.offer(new RSSI2DistInternalBufferRaw(a[i][0]));
  252. }
  253. this.raw = Collections.unmodifiableList(tmpList);
  254. this.stats = null;
  255. }
  256. /**
  257. * Load raw data from 2D integer array
  258. * [1d] = only sequence number
  259. * [2d] = only 1 element, rssi value
  260. *
  261. * Suitable when extracting raw data from resultset containing only RSSI values
  262. * @param a
  263. */
  264. public void loadData(Object[][] a){
  265. if (a==null){
  266. throw new IllegalArgumentException("Null object passed");
  267. }
  268. this.clear();
  269. LinkedList<RSSI2DistInternalBufferRaw> tmpList = new LinkedList<RSSI2DistInternalBufferRaw>();
  270. for(int i=0, cn=a.length; i<cn; i++){
  271. if (a[i]==null || a[i].length<1 || a[i].length>1){
  272. throw new IllegalArgumentException("Subarray is illegal (null or does not have correct size");
  273. }
  274. if (!(a[i][0] instanceof Integer)){
  275. throw new IllegalArgumentException("Illegal type, Integer excepted");
  276. }
  277. tmpList.offer(new RSSI2DistInternalBufferRaw((Integer) a[i][0]));
  278. }
  279. this.raw = Collections.unmodifiableList(tmpList);
  280. this.stats = null;
  281. }
  282. /**
  283. * Export data as raw integers
  284. * Useful for chart generation
  285. *
  286. * @return
  287. */
  288. public List<Integer> exportData(){
  289. if (this.raw==null) return null;
  290. List<Integer> resultList = new LinkedList<Integer>();
  291. Iterator<RSSI2DistInternalBufferRaw> it = this.raw.iterator();
  292. while(it.hasNext()){
  293. resultList.add(it.next().getRssi());
  294. }
  295. return resultList;
  296. }
  297. /**
  298. * =========================================================================
  299. *
  300. * GETTERS + SETTERS
  301. *
  302. * =========================================================================
  303. */
  304. /**
  305. * Set raw objects and cleans statistics
  306. * @param raw
  307. */
  308. public void setRaw(LinkedList<RSSI2DistInternalBufferRaw> raw) {
  309. this.raw = raw;
  310. this.stats = null;
  311. }
  312. public int getDistance() {
  313. return distance;
  314. }
  315. public void setDistance(int distance) {
  316. this.distance = distance;
  317. }
  318. public int getMid() {
  319. return mid;
  320. }
  321. public void setMid(int mid) {
  322. this.mid = mid;
  323. }
  324. /**
  325. * Returns unmodifiable list
  326. * @return
  327. */
  328. public List<RSSI2DistInternalBufferRaw> getRaw() {
  329. return Collections.unmodifiableList(raw);
  330. }
  331. public int getReportingMote() {
  332. return reportingMote;
  333. }
  334. public void setReportingMote(int reportingMote) {
  335. this.reportingMote = reportingMote;
  336. }
  337. public RSSI2DistInternalBufferStats getStats() {
  338. return stats;
  339. }
  340. public int getTalkingMote() {
  341. return talkingMote;
  342. }
  343. public void setTalkingMote(int talkingMote) {
  344. this.talkingMote = talkingMote;
  345. }
  346. public int getTestno() {
  347. return testno;
  348. }
  349. public void setTestno(int testno) {
  350. this.testno = testno;
  351. }
  352. public int getTxpower() {
  353. return txpower;
  354. }
  355. public void setTxpower(int txpower) {
  356. this.txpower = txpower;
  357. }
  358. public double getXvalue() {
  359. return xvalue;
  360. }
  361. public void setXvalue(double xvalue) {
  362. this.xvalue = xvalue;
  363. }
  364. }