Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public class NumberInputImpl extends AbstractFieldImpl implements NumberInput {
@Nullable
private Boolean excludeMinimumCheck;
/** End **/
private Long exclusiveMinimumValue;
private Long exclusiveMaximumValue;
private Number exclusiveMinimumValue;
private Number exclusiveMaximumValue;

/** Deprecated methods not to be changed **/
@Override
@Nullable
public Long getMinimum() {
Expand All @@ -83,12 +84,38 @@ public Long getMaximum() {
@Override
@Nullable
public Long getExclusiveMaximum() {
return exclusiveMaximumValue;
return (Long) exclusiveMaximumValue;
}

@Override
@Nullable
public Long getExclusiveMinimum() {
return (Long) exclusiveMinimumValue;
}

/** End of Deprecated methods not to be changed **/

@Override
@Nullable
public Number getMinimumNumber() {
return ComponentUtils.parseNumber(minimumAsStr);
}

@Override
@Nullable
public Number getMaximumNumber() {
return ComponentUtils.parseNumber(maximumAsStr);
}

@Override
@Nullable
public Number getExclusiveMaximumNumber() {
return exclusiveMaximumValue;
}

@Override
@Nullable
public Number getExclusiveMinimumNumber() {
return exclusiveMinimumValue;
}

Expand All @@ -104,13 +131,23 @@ public Type getType() {

@PostConstruct
private void initNumberInput() {
exclusiveMaximumValue = ComponentUtils.getExclusiveValue(exclusiveMaximum, maximum, excludeMaximumCheck);
exclusiveMinimumValue = ComponentUtils.getExclusiveValue(exclusiveMinimum, minimum, excludeMinimumCheck);
Object tempExclusiveMaximumValue = ComponentUtils.getExclusiveValue(exclusiveMaximum, maximumAsStr != null ? maximumAsStr : maximum,
excludeMaximumCheck);
Object tempExclusiveMinimumValue = ComponentUtils.getExclusiveValue(exclusiveMinimum, minimumAsStr != null ? minimumAsStr : minimum,
excludeMinimumCheck);
if (tempExclusiveMaximumValue != null) {
exclusiveMaximumValue = ComponentUtils.parseNumber(tempExclusiveMaximumValue.toString());
}
if (tempExclusiveMinimumValue != null) {
exclusiveMinimumValue = ComponentUtils.parseNumber(tempExclusiveMinimumValue.toString());
}
// in json either, exclusiveMaximum or maximum should be present
if (exclusiveMaximumValue != null) {
maximumAsStr = null;
maximum = null;
}
if (exclusiveMinimumValue != null) {
minimumAsStr = null;
minimum = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

import org.osgi.annotation.versioning.ProviderType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
* A interface which specifies the different form number type constraints
*
* @deprecated Use {@link NumberConstraintV2} instead.
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@Deprecated
@ProviderType
public interface NumberConstraint {

Expand All @@ -34,7 +37,10 @@ public interface NumberConstraint {
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
Long getMinimum();
@JsonIgnore
default Long getMinimum() {
return null;
}

/**
* Returns the maximum value for the number. The constraint is applicable only for field with type number
Expand All @@ -43,7 +49,10 @@ public interface NumberConstraint {
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
Long getMaximum();
@JsonIgnore
default Long getMaximum() {
return null;
}

/**
* Returns the Maximum value (exclusive) that can be entered by the user.
Expand All @@ -52,7 +61,10 @@ public interface NumberConstraint {
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
Long getExclusiveMaximum();
@JsonIgnore
default Long getExclusiveMaximum() {
return null;
}

/**
* Returns the minimum value (exclusive) that can be entered by the user.
Expand All @@ -61,6 +73,9 @@ public interface NumberConstraint {
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
Long getExclusiveMinimum();
@JsonIgnore
default Long getExclusiveMinimum() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 Adobe
~
~ Licensed 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 com.adobe.cq.forms.core.components.models.form;

import org.osgi.annotation.versioning.ProviderType;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* An interface which specifies the different form number type constraints with Number type.
*
* @since com.adobe.cq.forms.core.components.models.form 5.6.4
*/
@ProviderType
public interface NumberConstraintV2 extends NumberConstraint {

/**
* Returns the minimum value for the number. The constraint is applicable only for field with type number.
*
* @return minimum value for the number
* @since com.adobe.cq.forms.core.components.models.form 5.6.4
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("minimum")
default Number getMinimumNumber() {
return null;
}

/**
* Returns the maximum value for the number. The constraint is applicable only for field with type number.
*
* @return maximum value for the number
* @since com.adobe.cq.forms.core.components.models.form 5.6.4
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("maximum")
default Number getMaximumNumber() {
return null;
}

/**
* Returns the Maximum value (exclusive) that can be entered by the user.
*
* @return maximum value (exclusive) for the number
* @since com.adobe.cq.forms.core.components.models.form 5.6.4
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("exclusiveMaximum")
default Number getExclusiveMaximumNumber() {
return null;
}

/**
* Returns the minimum value (exclusive) that can be entered by the user.
*
* @return minimum value (exclusive) for the number
* @since com.adobe.cq.forms.core.components.models.form 5.6.4
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("exclusiveMinimum")
default Number getExclusiveMinimumNumber() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@ConsumerType
public interface NumberInput extends Field, NumberConstraint {
public interface NumberInput extends Field, NumberConstraintV2 {
@Nullable
@JsonIgnore
default String getEditFormat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* </p>
*/

@Version("5.5.4") // aligning this with release/650 since af2-rest-api is compiled with 5.2.0 in release/650
@Version("5.6.4")
package com.adobe.cq.forms.core.components.models.form;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,23 @@ public abstract class AbstractFieldImpl extends AbstractBaseImpl implements Fiel
@Nullable
protected Date maximumDate;

/** Do not do any changes, this is just present for backward compatibility **/
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_MAXIMUM)
@Nullable
protected Long maximum;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_MINIMUM)
@Nullable
protected Long minimum;
/** Do not do any changes, this is just present for backward compatibility **/

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_MAXIMUM)
@Nullable
protected String maximumAsStr;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_MINIMUM)
@Nullable
protected String minimumAsStr;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_EXCLUSIVE_MINIMUM)
@Default(booleanValues = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ public static <T> T getExclusiveValue(Object exclusiveValue, T value, Object exc
}
}

/**
* Parses a given string value into a Number.
* The method attempts to parse the string as a Long first, and if that fails,
* it attempts to parse it as a Float. If both parsing attempts fail, it returns null.
*
* @param value the string value to be parsed, can be null
* @return the parsed Number (Long or Float), or null if the value cannot be parsed
*/
public static Number parseNumber(@Nullable String value) {
try {
return value != null ? Long.parseLong(value) : null;
} catch (NumberFormatException e) {
try {
return Float.parseFloat(value);
} catch (NumberFormatException ex) {
return null;
}
}
}

@NotNull
public static Object[] coerce(@NotNull BaseConstraint.Type type, @NotNull Object[] objArr) {
if (type.equals(type.NUMBER) || type.equals(type.NUMBER_ARRAY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.adobe.cq.forms.core.components.models.form.ConstraintType;
import com.adobe.cq.forms.core.components.models.form.FieldType;
import com.adobe.cq.forms.core.components.models.form.Label;
import com.adobe.cq.forms.core.components.models.form.NumberConstraint;
import com.adobe.cq.forms.core.components.models.form.NumberConstraintV2;
import com.adobe.cq.forms.core.components.models.form.NumberInput;
import com.adobe.cq.forms.core.context.FormsCoreComponentTestContext;
import com.adobe.cq.wcm.style.ComponentStyleInfo;
Expand Down Expand Up @@ -309,38 +311,68 @@ void testIsShortDescriptionVisible() {
void testGetMinimum() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_CUSTOMIZED, NumberInput.class, context);
assertEquals(10000L, numberInput.getMinimum().longValue());
assertEquals(10000L, numberInput.getMinimumNumber().longValue());
NumberConstraint numberInputConstraintMock = Mockito.mock(NumberConstraint.class);
NumberConstraintV2 numberInputConstraintMock2 = Mockito.mock(NumberConstraintV2.class);
Mockito.when(numberInputConstraintMock.getMinimum()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock.getMinimum());
Mockito.when(numberInputConstraintMock2.getMinimumNumber()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock2.getMinimumNumber());
}

@Test
void testGetMaximum() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_CUSTOMIZED, NumberInput.class, context);
assertEquals(2000000, numberInput.getMaximumNumber().longValue());
assertEquals(2000000, numberInput.getMaximum().longValue());
NumberConstraint numberInputConstraintMock = Mockito.mock(NumberConstraint.class);
NumberConstraintV2 numberInputConstraintMock2 = Mockito.mock(NumberConstraintV2.class);
Mockito.when(numberInputConstraintMock.getMaximum()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock.getMaximum());
Mockito.when(numberInputConstraintMock2.getMaximumNumber()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock2.getMaximumNumber());
}

@Test
void testGetExclusiveMinimum() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_CONSTRAINTS, NumberInput.class, context);
assertEquals(10002L, numberInput.getExclusiveMinimum().longValue());
assertEquals(10002L, numberInput.getExclusiveMinimumNumber().longValue());
NumberConstraint numberInputConstraintMock = Mockito.mock(NumberConstraint.class);
NumberConstraintV2 numberInputConstraintMock2 = Mockito.mock(NumberConstraintV2.class);
Mockito.when(numberInputConstraintMock.getExclusiveMinimum()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock.getExclusiveMinimum());
Mockito.when(numberInputConstraintMock2.getExclusiveMinimumNumber()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock2.getExclusiveMinimumNumber());

}

@Test
void testGetExclusiveMaximum() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_CONSTRAINTS, NumberInput.class, context);
assertEquals(2000002, numberInput.getExclusiveMaximum().longValue());
assertEquals(2000002, numberInput.getExclusiveMaximumNumber().longValue());
NumberConstraint numberInputConstraintMock = Mockito.mock(NumberConstraint.class);
NumberConstraintV2 numberInputConstraintMock2 = Mockito.mock(NumberConstraintV2.class);
Mockito.when(numberInputConstraintMock.getExclusiveMaximum()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock.getExclusiveMaximum());
Mockito.when(numberInputConstraintMock2.getExclusiveMaximumNumber()).thenCallRealMethod();
assertEquals(null, numberInputConstraintMock2.getExclusiveMaximumNumber());
}

@Test
void testGetExclusiveMinimum_BC() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_BACKWARD_COMPATIBLE_STRING, NumberInput.class, context);
assertNull(numberInput.getMinimum());
assertEquals(10002L, numberInput.getExclusiveMinimum().longValue());
assertEquals(10002L, numberInput.getExclusiveMinimumNumber().longValue());
}

@Test
void testGetExclusiveMaximum_BC() {
NumberInput numberInput = Utils.getComponentUnderTest(PATH_NUMBER_INPUT_BACKWARD_COMPATIBLE_STRING, NumberInput.class, context);
assertNull(numberInput.getMaximum());
assertEquals(2000002, numberInput.getExclusiveMaximum().longValue());
assertEquals(2000002, numberInput.getExclusiveMaximumNumber().longValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ public void testIsFragmentComponent() {
assertFalse(ComponentUtils.isFragmentComponent(resource));
}

@Test
public void testParseNumber() {
// Test valid long string
assertEquals(123L, ComponentUtils.parseNumber("123"));
// Test valid float string
assertEquals(123.45f, ComponentUtils.parseNumber("123.45"));
// Test invalid number string
assertNull(ComponentUtils.parseNumber("abc"));
// Test null input
assertNull(ComponentUtils.parseNumber(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"$event.payload"
]
},
"minimum" : 100.22,
"maximum" : 102.44,
"properties": {
"fd:dor": {
"dorExclusion": false
Expand Down
Loading