You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/05/31 01:53:46 UTC
[07/14] james-project git commit: MAILBOX-338 ValueWithUnit is only
used for size
MAILBOX-338 ValueWithUnit is only used for size
We should:
- Move it to a more generic package
- Rename the class and method to make it size specific
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e3751bea
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e3751bea
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e3751bea
Branch: refs/heads/master
Commit: e3751bea31a93470e7bff47f21b162bbef8d9569
Parents: 905e942
Author: benwa <bt...@linagora.com>
Authored: Fri May 25 08:57:54 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu May 31 08:53:16 2018 +0700
----------------------------------------------------------------------
server/container/cli/pom.xml | 4 +
.../java/org/apache/james/cli/ServerCmd.java | 24 ++--
.../apache/james/cli/utils/ValueWithUnit.java | 128 -------------------
.../james/cli/utils/ValueWithUnitTest.java | 73 -----------
.../main/java/org/apache/james/util/Size.java | 128 +++++++++++++++++++
.../java/org/apache/james/util/SizeTest.java | 73 +++++++++++
6 files changed, 217 insertions(+), 213 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/cli/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/cli/pom.xml b/server/container/cli/pom.xml
index fe215fc..a6a0845 100644
--- a/server/container/cli/pom.xml
+++ b/server/container/cli/pom.xml
@@ -46,6 +46,10 @@
<artifactId>james-server-mailbox-adapter</artifactId>
</dependency>
<dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>james-server-util</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
----------------------------------------------------------------------
diff --git a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
index 19f0981..c6ac5e4 100644
--- a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
+++ b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
@@ -44,7 +44,6 @@ import org.apache.james.cli.probe.impl.JmxMailboxProbe;
import org.apache.james.cli.probe.impl.JmxQuotaProbe;
import org.apache.james.cli.probe.impl.JmxSieveProbe;
import org.apache.james.cli.type.CmdType;
-import org.apache.james.cli.utils.ValueWithUnit;
import org.apache.james.mailbox.quota.QuotaCount;
import org.apache.james.mailbox.quota.QuotaSize;
import org.apache.james.mailbox.quota.QuotaValue;
@@ -56,6 +55,7 @@ import org.apache.james.mailbox.store.probe.SieveProbe;
import org.apache.james.probe.DataProbe;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.util.Port;
+import org.apache.james.util.Size;
import org.apache.james.util.SizeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -312,10 +312,10 @@ public class ServerCmd {
mailboxProbe.reIndexAll();
break;
case SETSIEVEQUOTA:
- sieveProbe.setSieveQuota(ValueWithUnit.parse(arguments[1]).getConvertedValue());
+ sieveProbe.setSieveQuota(Size.parse(arguments[1]).asBytes());
break;
case SETSIEVEUSERQUOTA:
- sieveProbe.setSieveQuota(arguments[1], ValueWithUnit.parse(arguments[2]).getConvertedValue());
+ sieveProbe.setSieveQuota(arguments[1], Size.parse(arguments[2]).asBytes());
break;
case GETSIEVEQUOTA:
printStream.println("Storage space allowed for Sieve scripts by default: "
@@ -339,7 +339,7 @@ public class ServerCmd {
}
private SerializableQuotaValue<QuotaSize> parseQuotaSize(String argument) throws Exception {
- long convertedValue = ValueWithUnit.parse(argument).getConvertedValue();
+ long convertedValue = Size.parse(argument).asBytes();
return longToSerializableQuotaValue(convertedValue, QuotaSize.unlimited(), QuotaSize::size);
}
@@ -388,10 +388,10 @@ public class ServerCmd {
private String formatStorageValue(Long value) {
if (value == null) {
- return ValueWithUnit.UNKNOWN;
+ return Size.UNKNOWN;
}
if (value == SerializableQuota.UNLIMITED) {
- return ValueWithUnit.UNLIMITED;
+ return Size.UNLIMITED;
}
return SizeFormat.format(value);
}
@@ -401,18 +401,18 @@ public class ServerCmd {
.toValue(QuotaSize::size, QuotaSize.unlimited())
.map(size -> {
if (size.isUnlimited()) {
- return ValueWithUnit.UNLIMITED;
+ return Size.UNLIMITED;
}
return SizeFormat.format(size.asLong());
- }).orElse(ValueWithUnit.UNKNOWN);
+ }).orElse(Size.UNKNOWN);
}
private String formatMessageValue(Long value) {
if (value == null) {
- return ValueWithUnit.UNKNOWN;
+ return Size.UNKNOWN;
}
if (value == SerializableQuota.UNLIMITED) {
- return ValueWithUnit.UNLIMITED;
+ return Size.UNLIMITED;
}
return String.valueOf(value);
}
@@ -422,10 +422,10 @@ public class ServerCmd {
.toValue(QuotaCount::count, QuotaCount.unlimited())
.map(count -> {
if (count.isUnlimited()) {
- return ValueWithUnit.UNLIMITED;
+ return Size.UNLIMITED;
}
return String.valueOf(count.asLong());
- }).orElse(ValueWithUnit.UNKNOWN);
+ }).orElse(Size.UNKNOWN);
}
private void print(Map<String, Mappings> map, PrintStream out) {
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/cli/src/main/java/org/apache/james/cli/utils/ValueWithUnit.java
----------------------------------------------------------------------
diff --git a/server/container/cli/src/main/java/org/apache/james/cli/utils/ValueWithUnit.java b/server/container/cli/src/main/java/org/apache/james/cli/utils/ValueWithUnit.java
deleted file mode 100644
index b61d653..0000000
--- a/server/container/cli/src/main/java/org/apache/james/cli/utils/ValueWithUnit.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one *
- * or more contributor license agreements. See the NOTICE file *
- * distributed with this work for additional information *
- * regarding copyright ownership. The ASF licenses this file *
- * to you under the Apache License, Version 2.0 (the *
- * "License"); you may not use this file except in compliance *
- * with the License. You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- ****************************************************************/
-
-package org.apache.james.cli.utils;
-
-import com.google.common.math.LongMath;
-
-/**
- * This class is an helper for parsing integer input that may contain units.
- */
-public class ValueWithUnit {
-
- public static final String UNKNOWN = "UNKNOWN";
- public static final String UNLIMITED = "UNLIMITED";
- public static final long UNKNOWN_VALUE = Long.MIN_VALUE;
- public static final long UNLIMITED_VALUE = -1;
-
- /**
- * supported units : B ( 2^0 ), K ( 2^10 ), M ( 2^20 ), G ( 2^30 )
- * See RFC822.SIZE
- */
- private enum Unit {
- NoUnit,
- B,
- K,
- M,
- G
- }
-
- private static final long base = 1024;
-
- Unit unit;
- Long value;
-
- private ValueWithUnit(Unit unit, Long value) {
- this.unit = unit;
- this.value = value;
- }
-
- public static ValueWithUnit parse(String providedLongWithUnitString) throws Exception {
- if (providedLongWithUnitString.equalsIgnoreCase(UNKNOWN)) {
- return new ValueWithUnit(Unit.NoUnit, UNKNOWN_VALUE);
- }
- if (providedLongWithUnitString.equalsIgnoreCase(UNLIMITED)) {
- return new ValueWithUnit(Unit.NoUnit, UNLIMITED_VALUE);
- }
- char lastChar = providedLongWithUnitString.charAt(providedLongWithUnitString.length() - 1);
- Unit unit = getUnit(lastChar);
- String argWithoutUnit = removeLastCharIfNeeded(providedLongWithUnitString, unit);
- return new ValueWithUnit(unit, Long.parseLong(argWithoutUnit));
- }
-
- public Unit getUnit() {
- return unit;
- }
-
- public Long getValue() {
- return value;
- }
-
- public long getConvertedValue() {
- switch (unit) {
- case G:
- return value * LongMath.pow(base, 3);
- case M:
- return value * LongMath.pow(base, 2);
- case K:
- return value * LongMath.pow(base, 1);
- default:
- return value;
- }
- }
-
- private static String removeLastCharIfNeeded(String providedLongWithUnitString, Unit unit) {
- if (unit != Unit.NoUnit) {
- return providedLongWithUnitString.substring(0, providedLongWithUnitString.length() - 1);
- } else {
- return providedLongWithUnitString;
- }
- }
-
- private static Unit getUnit(char lastChar) throws Exception {
- switch (lastChar) {
- case 'K' :
- case 'k' :
- return Unit.K;
- case 'M' :
- case 'm' :
- return Unit.M;
- case 'G' :
- case 'g' :
- return Unit.G;
- case 'b' :
- case 'B' :
- return Unit.B;
- case '1' :
- case '2' :
- case '3' :
- case '4' :
- case '5' :
- case '6' :
- case '7' :
- case '8' :
- case '9' :
- case '0' :
- return Unit.NoUnit;
- default:
- throw new Exception("No unit corresponding to char : " + lastChar);
- }
- }
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/cli/src/test/java/org/apache/james/cli/utils/ValueWithUnitTest.java
----------------------------------------------------------------------
diff --git a/server/container/cli/src/test/java/org/apache/james/cli/utils/ValueWithUnitTest.java b/server/container/cli/src/test/java/org/apache/james/cli/utils/ValueWithUnitTest.java
deleted file mode 100644
index fe897dc..0000000
--- a/server/container/cli/src/test/java/org/apache/james/cli/utils/ValueWithUnitTest.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one *
- * or more contributor license agreements. See the NOTICE file *
- * distributed with this work for additional information *
- * regarding copyright ownership. The ASF licenses this file *
- * to you under the Apache License, Version 2.0 (the *
- * "License"); you may not use this file except in compliance *
- * with the License. You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- ****************************************************************/
-
-package org.apache.james.cli.utils;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.junit.Test;
-
-public class ValueWithUnitTest {
-
- @Test
- public void testNoUnit() throws Exception {
- assertThat(ValueWithUnit.parse("1024").getConvertedValue()).isEqualTo(1024);
- }
-
- @Test
- public void testUnitB() throws Exception {
- assertThat(ValueWithUnit.parse("1024B").getConvertedValue()).isEqualTo(1024);
- }
-
- @Test
- public void testUnitK() throws Exception {
- assertThat(ValueWithUnit.parse("5K").getConvertedValue()).isEqualTo(5 * 1024);
- }
-
- @Test
- public void testUnitM() throws Exception {
- assertThat(ValueWithUnit.parse("5M").getConvertedValue()).isEqualTo(5 * 1024 * 1024);
- }
-
- @Test
- public void testUnitG() throws Exception {
- assertThat(ValueWithUnit.parse("1G").getConvertedValue()).isEqualTo(1024 * 1024 * 1024);
- }
-
- @Test
- public void testUnknown() throws Exception {
- assertThat(ValueWithUnit.parse("unknown").getConvertedValue()).isEqualTo(ValueWithUnit.UNKNOWN_VALUE);
- }
-
- @Test
- public void testUnlimited() throws Exception {
- assertThat(ValueWithUnit.parse("unlimited").getConvertedValue()).isEqualTo(ValueWithUnit.UNLIMITED_VALUE);
- }
-
- @Test(expected = Exception.class)
- public void testBadUnit() throws Exception {
- ValueWithUnit.parse("42T");
- }
-
- @Test(expected = NumberFormatException.class)
- public void testWrongNumber() throws Exception {
- ValueWithUnit.parse("42RG");
- }
-
-}
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/util/src/main/java/org/apache/james/util/Size.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/Size.java b/server/container/util/src/main/java/org/apache/james/util/Size.java
new file mode 100644
index 0000000..341a0e7
--- /dev/null
+++ b/server/container/util/src/main/java/org/apache/james/util/Size.java
@@ -0,0 +1,128 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.util;
+
+import com.google.common.math.LongMath;
+
+/**
+ * This class is an helper for parsing integer input that may contain units.
+ */
+public class Size {
+
+ public static final String UNKNOWN = "UNKNOWN";
+ public static final String UNLIMITED = "UNLIMITED";
+ public static final long UNKNOWN_VALUE = Long.MIN_VALUE;
+ public static final long UNLIMITED_VALUE = -1;
+
+ /**
+ * supported units : B ( 2^0 ), K ( 2^10 ), M ( 2^20 ), G ( 2^30 )
+ * See RFC822.SIZE
+ */
+ private enum Unit {
+ NoUnit,
+ B,
+ K,
+ M,
+ G
+ }
+
+ private static final long base = 1024;
+
+ Unit unit;
+ Long value;
+
+ private Size(Unit unit, Long value) {
+ this.unit = unit;
+ this.value = value;
+ }
+
+ public static Size parse(String providedLongWithUnitString) throws Exception {
+ if (providedLongWithUnitString.equalsIgnoreCase(UNKNOWN)) {
+ return new Size(Unit.NoUnit, UNKNOWN_VALUE);
+ }
+ if (providedLongWithUnitString.equalsIgnoreCase(UNLIMITED)) {
+ return new Size(Unit.NoUnit, UNLIMITED_VALUE);
+ }
+ char lastChar = providedLongWithUnitString.charAt(providedLongWithUnitString.length() - 1);
+ Unit unit = getUnit(lastChar);
+ String argWithoutUnit = removeLastCharIfNeeded(providedLongWithUnitString, unit);
+ return new Size(unit, Long.parseLong(argWithoutUnit));
+ }
+
+ public Unit getUnit() {
+ return unit;
+ }
+
+ public Long getValue() {
+ return value;
+ }
+
+ public long asBytes() {
+ switch (unit) {
+ case G:
+ return value * LongMath.pow(base, 3);
+ case M:
+ return value * LongMath.pow(base, 2);
+ case K:
+ return value * LongMath.pow(base, 1);
+ default:
+ return value;
+ }
+ }
+
+ private static String removeLastCharIfNeeded(String providedLongWithUnitString, Unit unit) {
+ if (unit != Unit.NoUnit) {
+ return providedLongWithUnitString.substring(0, providedLongWithUnitString.length() - 1);
+ } else {
+ return providedLongWithUnitString;
+ }
+ }
+
+ private static Unit getUnit(char lastChar) throws Exception {
+ switch (lastChar) {
+ case 'K' :
+ case 'k' :
+ return Unit.K;
+ case 'M' :
+ case 'm' :
+ return Unit.M;
+ case 'G' :
+ case 'g' :
+ return Unit.G;
+ case 'b' :
+ case 'B' :
+ return Unit.B;
+ case '1' :
+ case '2' :
+ case '3' :
+ case '4' :
+ case '5' :
+ case '6' :
+ case '7' :
+ case '8' :
+ case '9' :
+ case '0' :
+ return Unit.NoUnit;
+ default:
+ throw new Exception("No unit corresponding to char : " + lastChar);
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/james-project/blob/e3751bea/server/container/util/src/test/java/org/apache/james/util/SizeTest.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/test/java/org/apache/james/util/SizeTest.java b/server/container/util/src/test/java/org/apache/james/util/SizeTest.java
new file mode 100644
index 0000000..0aaeae2
--- /dev/null
+++ b/server/container/util/src/test/java/org/apache/james/util/SizeTest.java
@@ -0,0 +1,73 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+
+public class SizeTest {
+
+ @Test
+ public void testNoUnit() throws Exception {
+ assertThat(Size.parse("1024").asBytes()).isEqualTo(1024);
+ }
+
+ @Test
+ public void testUnitB() throws Exception {
+ assertThat(Size.parse("1024B").asBytes()).isEqualTo(1024);
+ }
+
+ @Test
+ public void testUnitK() throws Exception {
+ assertThat(Size.parse("5K").asBytes()).isEqualTo(5 * 1024);
+ }
+
+ @Test
+ public void testUnitM() throws Exception {
+ assertThat(Size.parse("5M").asBytes()).isEqualTo(5 * 1024 * 1024);
+ }
+
+ @Test
+ public void testUnitG() throws Exception {
+ assertThat(Size.parse("1G").asBytes()).isEqualTo(1024 * 1024 * 1024);
+ }
+
+ @Test
+ public void testUnknown() throws Exception {
+ assertThat(Size.parse("unknown").asBytes()).isEqualTo(Size.UNKNOWN_VALUE);
+ }
+
+ @Test
+ public void testUnlimited() throws Exception {
+ assertThat(Size.parse("unlimited").asBytes()).isEqualTo(Size.UNLIMITED_VALUE);
+ }
+
+ @Test(expected = Exception.class)
+ public void testBadUnit() throws Exception {
+ Size.parse("42T");
+ }
+
+ @Test(expected = NumberFormatException.class)
+ public void testWrongNumber() throws Exception {
+ Size.parse("42RG");
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org