Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.pulsar.admin.cli.utils;

import com.beust.jcommander.ParameterException;
import org.apache.commons.lang3.StringUtils;

public class ValueValidationUtils {
private ValueValidationUtils() {
}

public static void maxValueCheck(String paramName, long value, long maxValue) {
if (value > maxValue) {
throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!");
}
}

public static void positiveCheck(String paramName, long value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void positiveCheck(String paramName, int value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void emptyCheck(String paramName, String value) {
if (StringUtils.isEmpty(value)) {
throw new ParameterException("The value of " + paramName + " can't be empty");
}
}

public static void minValueCheck(String name, Long value, long min) {
if (value < min) {
throw new ParameterException(name + " cannot be less than <" + min + ">!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.pulsar.admin.cli.utils.converters;

import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck;
import static org.apache.pulsar.admin.cli.utils.converters.ByteUnitUtil.validateSizeString;
import com.beust.jcommander.converters.BaseConverter;

public class ByteUnitIntegerConverter extends BaseConverter<Integer> {

public ByteUnitIntegerConverter(String optionName) {
super(optionName);
}

@Override
public Integer convert(String argStr) {
return parseBytes(argStr).intValue();
}

Long parseBytes(String argStr) {
emptyCheck(getOptionName(), argStr);
long valueInBytes = validateSizeString(argStr);
return valueInBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.pulsar.admin.cli.utils.converters;

import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck;
import com.beust.jcommander.converters.BaseConverter;

public class ByteUnitToLongConverter extends BaseConverter<Long> {

public ByteUnitToLongConverter(String optionName) {
super(optionName);
}

@Override
public Long convert(String argStr) {
return parseBytes(argStr);
}

Long parseBytes(String argStr) {
emptyCheck(getOptionName(), argStr);
return ByteUnitUtil.validateSizeString(argStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.pulsar.admin.cli.utils.converters;

import com.beust.jcommander.ParameterException;
import com.google.common.collect.Sets;
import java.util.Set;

class ByteUnitUtil {

private static Set<Character> sizeUnit = Sets.newHashSet('k', 'K', 'm', 'M', 'g', 'G', 't', 'T');

static long validateSizeString(String s) {
char last = s.charAt(s.length() - 1);
String subStr = s.substring(0, s.length() - 1);
long size;
try {
size = sizeUnit.contains(last)
? Long.parseLong(subStr)
: Long.parseLong(s);
} catch (IllegalArgumentException e) {
throw new ParameterException(String.format("Invalid size '%s'. Valid formats are: %s",
s, "(4096, 100K, 10M, 16G, 2T)"));
}
switch (last) {
case 'k':
case 'K':
return size * 1024;

case 'm':
case 'M':
return size * 1024 * 1024;

case 'g':
case 'G':
return size * 1024 * 1024 * 1024;

case 't':
case 'T':
return size * 1024 * 1024 * 1024 * 1024;

default:
return size;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.pulsar.admin.cli.utils.converters;

import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.converters.BaseConverter;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.common.util.RelativeTimeUtil;

public class TimeUnitToMillisConverter extends BaseConverter<Long> {

public TimeUnitToMillisConverter(String optionName) {
super(optionName);
}

@Override
public Long convert(String str) {
emptyCheck(getOptionName(), str);
try {
return TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(str.trim()));
} catch (IllegalArgumentException exception) {
throw new ParameterException("For input " + getOptionName() + ": " + exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.pulsar.admin.cli.utils.converters;

import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.converters.BaseConverter;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.common.util.RelativeTimeUtil;

public class TimeUnitToSecondsConverter extends BaseConverter<Long> {

public TimeUnitToSecondsConverter(String optionName) {
super(optionName);
}

@Override
public Long convert(String str) {
emptyCheck(getOptionName(), str);
try {
return TimeUnit.SECONDS.toSeconds(
RelativeTimeUtil.parseRelativeTimeInSeconds(str.trim()));
} catch (IllegalArgumentException exception) {
throw new ParameterException("For input " + getOptionName() + ": " + exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.pulsar.admin.cli.utils.converters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.pulsar.admin.cli.utils.validators;

import com.beust.jcommander.IValueValidator;
import com.beust.jcommander.ParameterException;
import org.apache.pulsar.admin.cli.utils.ValueValidationUtils;

public class IntegerMaxValueLongValidator implements IValueValidator<Long> {
@Override
public void validate(String name, Long value) throws ParameterException {
ValueValidationUtils.maxValueCheck(name, value, Integer.MAX_VALUE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.pulsar.admin.cli.utils.validators;

import com.beust.jcommander.IValueValidator;
import com.beust.jcommander.ParameterException;
import org.apache.pulsar.admin.cli.utils.ValueValidationUtils;

public class MinNegativeOneValidator implements IValueValidator<Long> {
@Override
public void validate(String name, Long value) throws ParameterException {
ValueValidationUtils.minValueCheck(name, value, -1L);
}
}
Loading