mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Add an exception in PossibleValueSource.withValuesFromEnum if duplicated id values are given
This commit is contained in:
@ -196,7 +196,8 @@ public class MetaDataProducerHelper
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
private static MetaDataProducerInterface<?> processMetaDataProducingPossibleValueEnum(Class<?> aClass)
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T extends PossibleValueEnum<T>> MetaDataProducerInterface<?> processMetaDataProducingPossibleValueEnum(Class<?> aClass)
|
||||
{
|
||||
String warningPrefix = "Found a class annotated as @" + QMetaDataProducingPossibleValueEnum.class.getSimpleName();
|
||||
if(!PossibleValueEnum.class.isAssignableFrom(aClass))
|
||||
@ -206,7 +207,7 @@ public class MetaDataProducerHelper
|
||||
}
|
||||
|
||||
PossibleValueEnum<?>[] values = (PossibleValueEnum<?>[]) aClass.getEnumConstants();
|
||||
return (new PossibleValueSourceOfEnumGenericMetaDataProducer<>(aClass.getSimpleName(), values));
|
||||
return (new PossibleValueSourceOfEnumGenericMetaDataProducer<T>(aClass.getSimpleName(), (PossibleValueEnum<T>[]) values));
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,7 +23,10 @@ package com.kingsrook.qqq.backend.core.model.metadata.possiblevalues;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.TopLevelMetaDataInterface;
|
||||
@ -97,7 +100,7 @@ public class QPossibleValueSource implements TopLevelMetaDataInterface
|
||||
** Create a new possible value source, for an enum, with default settings.
|
||||
** e.g., type=ENUM; name from param values from the param; LABEL_ONLY format
|
||||
*******************************************************************************/
|
||||
public static <T extends PossibleValueEnum<?>> QPossibleValueSource newForEnum(String name, T[] values)
|
||||
public static <I, T extends PossibleValueEnum<I>> QPossibleValueSource newForEnum(String name, T[] values)
|
||||
{
|
||||
return new QPossibleValueSource()
|
||||
.withName(name)
|
||||
@ -553,11 +556,25 @@ public class QPossibleValueSource implements TopLevelMetaDataInterface
|
||||
** myPossibleValueSource.withValuesFromEnum(MyEnum.values()));
|
||||
**
|
||||
*******************************************************************************/
|
||||
public <T extends PossibleValueEnum<?>> QPossibleValueSource withValuesFromEnum(T[] values)
|
||||
public <I, T extends PossibleValueEnum<I>> QPossibleValueSource withValuesFromEnum(T[] values)
|
||||
{
|
||||
Set<I> usedIds = new HashSet<>();
|
||||
List<I> duplicatedIds = new ArrayList<>();
|
||||
|
||||
for(T t : values)
|
||||
{
|
||||
if(usedIds.contains(t.getPossibleValueId()))
|
||||
{
|
||||
duplicatedIds.add(t.getPossibleValueId());
|
||||
}
|
||||
|
||||
addEnumValue(new QPossibleValue<>(t.getPossibleValueId(), t.getPossibleValueLabel()));
|
||||
usedIds.add(t.getPossibleValueId());
|
||||
}
|
||||
|
||||
if(!duplicatedIds.isEmpty())
|
||||
{
|
||||
throw (new QRuntimeException("Error: Duplicated id(s) found in enum values: " + duplicatedIds));
|
||||
}
|
||||
|
||||
return (this);
|
||||
|
@ -37,7 +37,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleVal
|
||||
public class PossibleValueSourceOfEnumGenericMetaDataProducer<T extends PossibleValueEnum<T>> implements MetaDataProducerInterface<QPossibleValueSource>
|
||||
{
|
||||
private final String name;
|
||||
private final PossibleValueEnum<?>[] values;
|
||||
private final PossibleValueEnum<T>[] values;
|
||||
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ public class PossibleValueSourceOfEnumGenericMetaDataProducer<T extends Possible
|
||||
** Constructor
|
||||
**
|
||||
*******************************************************************************/
|
||||
public PossibleValueSourceOfEnumGenericMetaDataProducer(String name, PossibleValueEnum<?>[] values)
|
||||
public PossibleValueSourceOfEnumGenericMetaDataProducer(String name, PossibleValueEnum<T>[] values)
|
||||
{
|
||||
this.name = name;
|
||||
this.values = values;
|
||||
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2024. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.backend.core.model.metadata.possiblevalues;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Unit test for QPossibleValueSource
|
||||
*******************************************************************************/
|
||||
class QPossibleValueSourceTest extends BaseTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testWithValuesFromEnum()
|
||||
{
|
||||
assertThatThrownBy(() -> new QPossibleValueSource().withValuesFromEnum(DupeIds.values()))
|
||||
.isInstanceOf(QRuntimeException.class)
|
||||
.hasMessageContaining("Duplicated id(s)")
|
||||
.hasMessageMatching(".*: \\[1]$");
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
private enum DupeIds implements PossibleValueEnum<Integer>
|
||||
{
|
||||
ONE_A(1, "A"),
|
||||
TWO_B(2, "B"),
|
||||
ONE_C(1, "C");
|
||||
|
||||
|
||||
private final int id;
|
||||
private final String label;
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
DupeIds(int id, String label)
|
||||
{
|
||||
this.id = id;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
@Override
|
||||
public Integer getPossibleValueId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
@Override
|
||||
public String getPossibleValueLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user