'Java: addressing "Unchecked Overriding" like errors
I am trying to make a representation of a domain of arbitrary values. So far I have these definitions:
public interface IDomainValue<V extends Comparable<V>> extends Comparable<IDomainValue<V>> {
V getValue();
UUID getDomainId();
}
public interface IFiniteDomain<V extends Comparable<V>, T extends IDomainValue<V>> {
Iterable<T> getDomainValuesInSortedOrder();
Iterable<T> getDomainValuesInRandomOrder();
T getMin();
}
@NoArgsConstructor
public class DecimalDomain implements IFiniteDomain<Integer, DecimalDomain.DecimalDomainValue> {
private static final int RAW_MIN = 0;
private static final int RAW_MAX = 9;
private static final UUID DOMAIN_ID = UUID.fromString(DecimalDomain.class.getName());
// ImmutableList containing all domain values in sorted order.
private static final ImmutableList<DecimalDomainValue> domainValues = ImmutableList.<DecimalDomainValue>builder()
.addAll(IntStream.rangeClosed(RAW_MIN, RAW_MAX)
.boxed()
.sorted()
.map(val -> new DecimalDomainValue(val, DOMAIN_ID))
.collect(Collectors.toList()))
.build();
public Iterable<DecimalDomainValue> getDomainValuesInSortedOrder() {
return domainValues;
}
public Iterable<DecimalDomainValue> getDomainValuesInRandomOrder() {
final List<DecimalDomainValue> shuffledDomainValues = new ArrayList<>(domainValues);
Collections.shuffle(shuffledDomainValues, sourceOfRandomness);
return shuffledDomainValues;
}
public DecimalDomainValue getMin() {
return domainValues.get(RAW_MIN);
}
@AllArgsConstructor
static class DecimalDomainValue implements IDomainValue<Integer> {
@Getter
private final Integer value;
@Getter
private final UUID domainId;
@Override
public boolean equals(Object obj) {
// Implementation of equals here.
}
@Override
public int compareTo(IDomainValue<Integer> dv) {
// Implementation of compareTo here.
}
}
}
I have tried a few alternatives: IDomainValue<?> getMin(), <T extends IDomainValue<? extends Comparable>> T getMin(), among a few others, but can never resolve all of the type warnings/errors.
Effectively, I want to be able to iterate over values in my domain, as well as perform comparisons between them, without knowing the type V in V extends Comparable down the line. More specifically, I am working on a CSP solver where each variable in the CSP may have a different finite domain, and being able to iterate over possible values that this CSP variable can take (without having to know the type) would be useful to implement a backtracking-based solver.
My interface for CSP variables looks like
public interface IProblemVariable<V extends Comparable<V>, T extends IDomainValue<V>> {
IFiniteDomain<V, T> getDomain();
Optional<T> getValue();
void assignValue(T newValue);
void clearValue();
boolean isAssigned();
}
How should I set these up? Ideally, it would be simpler if I can avoid parameterizing IFiniteDomain/DecimalDomain. If I parameterize IFiniteDomain, then this seems to lead to needing a parameterization of the CSP solver, but each variable in the CSP could have a different domain.
---- Edit ----
A few additional snippets:
private SearchBasedCSPEngine(@Nonnull final Collection<IProblemVariable<?, ?>> problemVariables,
@Nonnull final Collection<IProblemConstraint> problemConstraints) {
vars = new HashSet<>(problemVariables);
constraints = new HashSet<>(problemConstraints);
unassignedProblemVariables = problemVariables.stream()
.filter(v -> !v.isAssigned())
.collect(Collectors.toCollection(LinkedList::new));
// Error cannot infer arguments for HashSet
candidateValues = unassignedProblemVariables.stream()
.collect(Collectors.toMap(v -> v, v -> new HashSet<>(v.getDomain().getDomainValuesInRandomOrder())));
}
IProblemVariable<?, ?> var = unassignedProblemVariables.poll();
List<?> possibleDomainValues = new LinkedList<>(candidateValues.get(var));
// Error here as well.
var.assignValue(possibleDomainValues.get(0));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
