Skip to content

Commit

Permalink
✨ 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunMengLu committed Jul 30, 2024
1 parent 3c98f39 commit a30bdc2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@Getter
@RequiredArgsConstructor
public class DataSize {
class DataSize {

/**
* Bytes per Kilobyte.
Expand All @@ -39,10 +39,6 @@ public class DataSize {

private final long bytes;

public DataSize(String text) {
this(parse(text).getBytes());
}

/**
* Obtain a {@link DataSize} representing the specified number of bytes.
* @param bytes the number of bytes, positive or negative
Expand Down Expand Up @@ -131,14 +127,6 @@ public static DataSize parse(String text) {
}
}

/**
* Return the number of bytes in this instance.
* @return the number of bytes
*/
public long toBytes() {
return this.bytes;
}

/**
* Static nested class to support lazy loading of the {@link #PATTERN}.
* @since 5.3.21
Expand All @@ -152,6 +140,56 @@ private static class DataSizeUtils {
private static DataUnit determineDataUnit(String suffix) {
return (StrUtil.isNotBlank(suffix) ? DataUnit.fromSuffix(suffix) : DataUnit.BYTES);
}
}

@Getter
@RequiredArgsConstructor
public enum DataUnit {

/**
* Bytes, represented by suffix {@code B}.
*/
BYTES("B", DataSize.ofBytes(1)),

/**
* Kilobytes, represented by suffix {@code KB}.
*/
KILOBYTES("KB", DataSize.ofKilobytes(1)),

/**
* Megabytes, represented by suffix {@code MB}.
*/
MEGABYTES("MB", DataSize.ofMegabytes(1)),

/**
* Gigabytes, represented by suffix {@code GB}.
*/
GIGABYTES("GB", DataSize.ofGigabytes(1)),

/**
* Terabytes, represented by suffix {@code TB}.
*/
TERABYTES("TB", DataSize.ofTerabytes(1));


private final String suffix;
private final DataSize size;

/**
* Return the {@link DataUnit} matching the specified {@code suffix}.
* @param suffix one of the standard suffixes
* @return the {@link DataUnit} matching the specified {@code suffix}
* @throws IllegalArgumentException if the suffix does not match the suffix
* of any of this enum's constants
*/
public static DataUnit fromSuffix(String suffix) {
for (DataUnit candidate : values()) {
if (candidate.suffix.equals(suffix)) {
return candidate;
}
}
throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public MqttClientCreator mqttClientCreator(MqttClientProperties properties) {
.username(properties.getUserName())
.password(properties.getPassword())
.clientId(properties.getClientId())
.readBufferSize((int) DataSize.parse(properties.getReadBufferSize()).toBytes())
.maxBytesInMessage((int) DataSize.parse(properties.getMaxBytesInMessage()).toBytes())
.readBufferSize((int) DataSize.parse(properties.getReadBufferSize()).getBytes())
.maxBytesInMessage((int) DataSize.parse(properties.getMaxBytesInMessage()).getBytes())
.maxClientIdLength(properties.getMaxClientIdLength())
.keepAliveSecs(properties.getKeepAliveSecs())
.reconnect(properties.isReconnect())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@Getter
@RequiredArgsConstructor
public class DataSize {
class DataSize {

/**
* Bytes per Kilobyte.
Expand All @@ -39,10 +39,6 @@ public class DataSize {

private final long bytes;

public DataSize(String text) {
this(parse(text).getBytes());
}

/**
* Obtain a {@link DataSize} representing the specified number of bytes.
* @param bytes the number of bytes, positive or negative
Expand Down Expand Up @@ -144,6 +140,56 @@ private static class DataSizeUtils {
private static DataUnit determineDataUnit(String suffix) {
return (StrUtil.isNotBlank(suffix) ? DataUnit.fromSuffix(suffix) : DataUnit.BYTES);
}
}

@Getter
@RequiredArgsConstructor
public enum DataUnit {

/**
* Bytes, represented by suffix {@code B}.
*/
BYTES("B", DataSize.ofBytes(1)),

/**
* Kilobytes, represented by suffix {@code KB}.
*/
KILOBYTES("KB", DataSize.ofKilobytes(1)),

/**
* Megabytes, represented by suffix {@code MB}.
*/
MEGABYTES("MB", DataSize.ofMegabytes(1)),

/**
* Gigabytes, represented by suffix {@code GB}.
*/
GIGABYTES("GB", DataSize.ofGigabytes(1)),

/**
* Terabytes, represented by suffix {@code TB}.
*/
TERABYTES("TB", DataSize.ofTerabytes(1));


private final String suffix;
private final DataSize size;

/**
* Return the {@link DataUnit} matching the specified {@code suffix}.
* @param suffix one of the standard suffixes
* @return the {@link DataUnit} matching the specified {@code suffix}
* @throws IllegalArgumentException if the suffix does not match the suffix
* of any of this enum's constants
*/
public static DataUnit fromSuffix(String suffix) {
for (DataUnit candidate : values()) {
if (candidate.suffix.equals(suffix)) {
return candidate;
}
}
throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
}
}

}

This file was deleted.

0 comments on commit a30bdc2

Please sign in to comment.