Fix some SpotBugs issues
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / fixedflex / FixedFlexImpl.java
1 /*
2  * Copyright © 2017 Orange, 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.transportpce.common.fixedflex;
10
11 public final class FixedFlexImpl implements FixedFlexInterface {
12     private long index;
13     private double centerFrequency;
14     private double start;
15     private double stop;
16     private double wavelength;
17     // wavelengthSpacing is in GHz
18     float wavelengthSpacing = 50;
19     // BEGIN_WAVELENGTH is in nm: f or F is appended to treat it explicitly as simple float (and not double float)
20     static final float BEGIN_WAVELENGTH = 1528.77f;
21     // java double is double float - d or D is appended to treat it explicitly as double float
22     static final double PRECISION = 10000d;
23
24     public FixedFlexImpl(Long index, double centreFrequency, double start, double stop, double wavelength) {
25         this.index = index;
26         this.centerFrequency = centreFrequency;
27         this.start = start;
28         this.stop = stop;
29         this.wavelength = wavelength;
30     }
31
32     public FixedFlexImpl() {
33         this.index = 0L;
34         this.centerFrequency = 0;
35         this.start = 0;
36         this.stop = 0;
37         this.wavelength = 0;
38     }
39
40     public FixedFlexImpl(long wlIndex) {
41         this.centerFrequency = 196.1 - (wlIndex - 1) * wavelengthSpacing / 1000;
42         // Truncate the value to the two decimal places
43         this.centerFrequency = Math.round(this.centerFrequency * PRECISION) / PRECISION;
44         this.start = this.centerFrequency - (wavelengthSpacing / 2) / 1000;
45         this.start = Math.round(this.start * PRECISION) / PRECISION;
46         this.stop = this.centerFrequency + (wavelengthSpacing / 2) / 1000;
47         this.stop = Math.round(this.stop * PRECISION) / PRECISION;
48         this.wavelength = BEGIN_WAVELENGTH + ((wlIndex - 1) * 0.40);
49         this.wavelength = Math.round(this.wavelength * PRECISION) / PRECISION;
50     }
51
52     @Override
53     /**
54      * @param index Wavelength number
55      * @return Returns FixedFlexImp object with the calculated result.
56      **/
57     public FixedFlexImpl getFixedFlexWaveMapping(long wlIndex) {
58         // In Flex grid  -35 <= n <= 60
59         long mappedWL = 61 - wlIndex;
60         FixedFlexImpl fixedFlex = new FixedFlexImpl();
61         fixedFlex.centerFrequency = 193.1 + (50.0 / 1000.0) * mappedWL;
62         fixedFlex.centerFrequency = Math.round(fixedFlex.centerFrequency * PRECISION) / PRECISION;
63         fixedFlex.wavelength = BEGIN_WAVELENGTH + ((wlIndex - 1) * 0.40);
64         fixedFlex.wavelength = Math.round(fixedFlex.wavelength * PRECISION) / PRECISION;
65         fixedFlex.start = 193.1 + (50.0 * mappedWL - 25) / 1000.0;
66         fixedFlex.start = Math.round(fixedFlex.start * PRECISION) / PRECISION;
67         fixedFlex.stop = 193.1 + (50.0 * mappedWL + 25) / 1000.0;
68         fixedFlex.stop = Math.round(fixedFlex.stop * PRECISION) / PRECISION;
69         fixedFlex.index = wlIndex;
70         return fixedFlex;
71     }
72
73     public double getCenterFrequency() {
74         return centerFrequency;
75     }
76
77     public double getStart() {
78         return start;
79     }
80
81     public long getIndex() {
82         return index;
83     }
84
85     public double getStop() {
86         return stop;
87     }
88
89     public double getWavelength() {
90         return wavelength;
91     }
92 }