Skip to content

Commit

Permalink
removed code smells detected by IntelliJ and Error Prone SCA, removed…
Browse files Browse the repository at this point in the history
… the GUI's jcalendar dependency
  • Loading branch information
sfeuerhahn committed Jun 30, 2020
1 parent 6c89bb3 commit 792afe0
Show file tree
Hide file tree
Showing 51 changed files with 316 additions and 348 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ tasks.register<Tar>("tar") {

dependencies {
implementation("com.beanit:asn1bean:1.12.0")
implementation("com.toedter:jcalendar:1.4")
implementation("org.slf4j:slf4j-api:1.7.25")
runtimeOnly("ch.qos.logback:logback-classic:1.2.3")
}
Expand Down
1 change: 0 additions & 1 deletion doc/iec61850bean-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Besides the IEC61850bean library the folder *build/libs-all/* contains the follo

* *logback-core/logback-classic* - an actual logger implementation of the slf4-api. It is used by the console server application to output log information. It can be replaced by a logger of your choice that supports the slf4j API. Like slf4j it is only needed for server implementations. License: EPLv1.0 and LGPLv2.1, http://logback.qos.ch

* *jcalendar* - a calendar library needed by the client GUI. You don't need this dependency if you don't use the client gui. (C)1999-2011 Kai Toedter, License: LGPLv3, http://toedter.com/jcalendar/


### Console & GUI Applications
Expand Down
5 changes: 5 additions & 0 deletions lastconnection.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Tue Jun 30 15:11:36 CEST 2020
tselLocal=0,0
serverAddress=127.0.0.1
tselRemote=0,1
serverPort=10002
3 changes: 1 addition & 2 deletions src/main/java/com/beanit/iec61850bean/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -81,7 +80,7 @@ public ModelNode copy() {

@Override
public List<BasicDataAttribute> getBasicDataAttributes() {
List<BasicDataAttribute> subBasicDataAttributes = new LinkedList<>();
List<BasicDataAttribute> subBasicDataAttributes = new ArrayList<>();
for (ModelNode item : items) {
subBasicDataAttributes.addAll(item.getBasicDataAttributes());
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/beanit/iec61850bean/BasicDataAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public abstract class BasicDataAttribute extends FcModelNode {
Expand All @@ -34,10 +33,10 @@ public abstract class BasicDataAttribute extends FcModelNode {
boolean qchg;
boolean dupd;

List<Urcb> chgRcbs = null;
List<Urcb> dupdRcbs = null;
final List<Urcb> chgRcbs;
final List<Urcb> dupdRcbs;

BasicDataAttribute(
protected BasicDataAttribute(
ObjectReference objectReference, Fc fc, String sAddr, boolean dchg, boolean dupd) {
this.objectReference = objectReference;
this.fc = fc;
Expand All @@ -47,9 +46,13 @@ public abstract class BasicDataAttribute extends FcModelNode {

if (dchg) {
chgRcbs = new ArrayList<>();
} else {
chgRcbs = null;
}
if (dupd) {
dupdRcbs = new ArrayList<>();
} else {
dupdRcbs = null;
}
}

Expand Down Expand Up @@ -97,7 +100,7 @@ public Iterator<ModelNode> iterator() {

@Override
public List<BasicDataAttribute> getBasicDataAttributes() {
List<BasicDataAttribute> subBasicDataAttributes = new LinkedList<>();
List<BasicDataAttribute> subBasicDataAttributes = new ArrayList<>();
subBasicDataAttributes.add(this);
return subBasicDataAttributes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beanit/iec61850bean/BdaBitString.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class BdaBitString extends BasicDataAttribute {
final int maxNumBits;
volatile byte[] value;

public BdaBitString(
protected BdaBitString(
ObjectReference objectReference,
Fc fc,
String sAddr,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beanit/iec61850bean/BdaEntryTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setTimestamp(long timestamp) {
(byte) (ms >> 24),
(byte) (ms >> 16),
(byte) (ms >> 8),
(byte) (ms),
(byte) ms,
(byte) (days >> 8),
(byte) days
};
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/beanit/iec61850bean/BdaFloat64.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.beanit.iec61850bean.internal.mms.asn1.TypeDescription;
import com.beanit.iec61850bean.internal.mms.asn1.Unsigned8;
import java.nio.ByteBuffer;
import java.util.Arrays;

public final class BdaFloat64 extends BasicDataAttribute {

Expand Down Expand Up @@ -55,14 +56,14 @@ public Double getDouble() {
return null;
}
return Double.longBitsToDouble(
((0xffL & (value[1])) << 56)
| ((0xffL & (value[2])) << 48)
| ((0xffL & (value[3])) << 40)
| ((0xffL & (value[4])) << 32)
| ((0xffL & (value[5])) << 24)
| ((0xffL & (value[6])) << 16)
| ((0xffL & (value[7])) << 8)
| ((0xffL & (value[8])) << 0));
((0xffL & value[1]) << 56)
| ((0xffL & value[2]) << 48)
| ((0xffL & value[3]) << 40)
| ((0xffL & value[4]) << 32)
| ((0xffL & value[5]) << 24)
| ((0xffL & value[6]) << 16)
| ((0xffL & value[7]) << 8)
| ((0xffL & value[8]) << 0));
}

public void setDouble(Double value) {
Expand Down Expand Up @@ -126,6 +127,6 @@ public String toString() {

@Override
public String getValueString() {
return "" + value;
return "" + Arrays.toString(value);
}
}
7 changes: 1 addition & 6 deletions src/main/java/com/beanit/iec61850bean/BdaQuality.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@
*/
package com.beanit.iec61850bean;

import java.util.ArrayList;

public final class BdaQuality extends BdaBitString {

public BdaQuality(ObjectReference objectReference, Fc fc, String sAddr, boolean qchg) {
super(objectReference, fc, sAddr, 13, false, false);
super(objectReference, fc, sAddr, 13, qchg, false);
this.qchg = qchg;
basicType = BdaType.QUALITY;
if (qchg) {
chgRcbs = new ArrayList<>();
}
setDefault();
}

Expand Down
82 changes: 30 additions & 52 deletions src/main/java/com/beanit/iec61850bean/BdaTimestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.beanit.iec61850bean.internal.mms.asn1.Data;
import com.beanit.iec61850bean.internal.mms.asn1.TypeDescription;
import com.beanit.iec61850bean.internal.mms.asn1.UtcTime;
import java.util.Calendar;
import java.time.Instant;
import java.util.Date;

public final class BdaTimestamp extends BasicDataAttribute {
Expand Down Expand Up @@ -65,43 +65,6 @@ private int getFractionOfSecond() {
return ((0xff & value[4]) << 16 | (0xff & value[5]) << 8 | (0xff & value[6]));
}

public void setDate(
Date date,
boolean leapSecondsKnown,
boolean clockFailure,
boolean clockNotSynchronized,
int timeAccuracy) {
if (value == null) {
value = new byte[8];
}

int secondsSinceEpoch = (int) (date.getTime() / 1000L);
int fractionOfSecond = (int) ((date.getTime() % 1000L) / 1000.0 * (1 << 24));

int timeQuality = timeAccuracy & 0x1f;
if (leapSecondsKnown) {
timeQuality = timeQuality | 0x80;
}
if (clockFailure) {
timeQuality = timeQuality | 0x40;
}
if (clockNotSynchronized) {
timeQuality = timeQuality | 0x20;
}

value =
new byte[] {
(byte) ((secondsSinceEpoch >> 24) & 0xff),
(byte) ((secondsSinceEpoch >> 16) & 0xff),
(byte) ((secondsSinceEpoch >> 8) & 0xff),
(byte) (secondsSinceEpoch & 0xff),
(byte) ((fractionOfSecond >> 16) & 0xff),
(byte) ((fractionOfSecond >> 8) & 0xff),
(byte) (fractionOfSecond & 0xff),
(byte) timeQuality
};
}

@Override
public void setValueFrom(BasicDataAttribute bda) {
byte[] srcValue = ((BdaTimestamp) bda).getValue();
Expand All @@ -111,26 +74,44 @@ public void setValueFrom(BasicDataAttribute bda) {
System.arraycopy(srcValue, 0, value, 0, srcValue.length);
}

public Date getDate() {
public Instant getInstant() {
if (value == null || value.length == 0) {
return null;
}
long time =
getSecondsSinceEpoch() * 1000L
+ (long) (((float) getFractionOfSecond()) / (1 << 24) * 1000 + 0.5);
return new Date(time);
return Instant.ofEpochMilli(time);
}

public void setDate(Date date) {
public void setInstant(Instant instant) {
setInstant(instant, true, false, false, 10);
}

public void setInstant(
Instant instant,
boolean leapSecondsKnown,
boolean clockFailure,
boolean clockNotSynchronized,
int timeAccuracy) {
if (value == null) {
value = new byte[8];
}

int secondsSinceEpoch = (int) (date.getTime() / 1000L);
int fractionOfSecond = (int) ((date.getTime() % 1000L) / 1000.0 * (1 << 24));
int secondsSinceEpoch = (int) (instant.toEpochMilli() / 1000L);
int fractionOfSecond = (int) ((instant.toEpochMilli() % 1000L) / 1000.0 * (1 << 24));

int timeQuality = timeAccuracy & 0x1f;
if (leapSecondsKnown) {
timeQuality = timeQuality | 0x80;
}
if (clockFailure) {
timeQuality = timeQuality | 0x40;
}
if (clockNotSynchronized) {
timeQuality = timeQuality | 0x20;
}

// 0x8a = time accuracy of 10 and LeapSecondsKnown = true, ClockFailure
// = false, ClockNotSynchronized = false
value =
new byte[] {
(byte) ((secondsSinceEpoch >> 24) & 0xff),
Expand All @@ -140,7 +121,7 @@ public void setDate(Date date) {
(byte) ((fractionOfSecond >> 16) & 0xff),
(byte) ((fractionOfSecond >> 8) & 0xff),
(byte) (fractionOfSecond & 0xff),
(byte) 0x8a
(byte) timeQuality
};
}

Expand All @@ -161,9 +142,6 @@ public void setValue(byte[] value) {
* does not take into account the leap seconds that occurred before the initialization of the time
* source of the device.
*
* <p>Java {@link Date} and {@link Calendar} objects do handle leap seconds, so this is usually
* true.
*
* @return TRUE of the attribute LeapSecondsKnown shall indicate that the value for
* SecondSinceEpoch takes into account all leap seconds occurred
*/
Expand Down Expand Up @@ -213,7 +191,7 @@ public void setDefault() {

/** Sets Timestamp to current time */
public void setCurrentTime() {
setDate(new Date());
setInstant(Instant.now());
}

@Override
Expand Down Expand Up @@ -254,11 +232,11 @@ TypeDescription getMmsTypeSpec() {

@Override
public String toString() {
return getReference().toString() + ": " + getDate();
return getReference().toString() + ": " + getInstant();
}

@Override
public String getValueString() {
return getDate().toString();
return getInstant().toString();
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/beanit/iec61850bean/BdaUnicodeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.beanit.iec61850bean;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.beanit.iec61850bean.internal.mms.asn1.Data;
import com.beanit.iec61850bean.internal.mms.asn1.Integer32;
import com.beanit.iec61850bean.internal.mms.asn1.MMSString;
Expand Down Expand Up @@ -108,6 +110,6 @@ public String toString() {
if (value == null) {
return getReference().toString() + ": null";
}
return getReference().toString() + ": " + new String(value);
return getReference().toString() + ": " + new String(value, UTF_8);
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/beanit/iec61850bean/BdaVisibleString.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.beanit.iec61850bean;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.beanit.asn1bean.ber.types.string.BerVisibleString;
import com.beanit.iec61850bean.internal.mms.asn1.Data;
import com.beanit.iec61850bean.internal.mms.asn1.Integer32;
Expand Down Expand Up @@ -49,7 +51,7 @@ public void setValue(byte[] value) {
}

public void setValue(String value) {
setValue(value.getBytes());
setValue(value.getBytes(UTF_8));
}

@Override
Expand All @@ -66,7 +68,7 @@ public int getMaxLength() {
}

public String getStringValue() {
return new String(value);
return new String(value, UTF_8);
}

@Override
Expand Down Expand Up @@ -118,11 +120,11 @@ public String toString() {
if (value.length == 0 || value[0] == (byte) 0) {
return getReference().toString() + ": ''";
}
return getReference().toString() + ": " + new String(value);
return getReference().toString() + ": " + new String(value, UTF_8);
}

@Override
public String getValueString() {
return new String(value);
return new String(value, UTF_8);
}
}
Loading

0 comments on commit 792afe0

Please sign in to comment.