@@ -564,11 +564,37 @@ public Setting<T> getConcreteSetting(String key) {
564
564
return this ;
565
565
}
566
566
567
+ /**
568
+ * Allows a setting to declare a dependency on another setting being set. Optionally, a setting can validate the value of the dependent
569
+ * setting.
570
+ */
571
+ public interface SettingDependency {
572
+
573
+ /**
574
+ * The setting to declare a dependency on.
575
+ *
576
+ * @return the setting
577
+ */
578
+ Setting getSetting ();
579
+
580
+ /**
581
+ * Validates the dependent setting value.
582
+ *
583
+ * @param key the key for this setting
584
+ * @param value the value of this setting
585
+ * @param dependency the value of the dependent setting
586
+ */
587
+ default void validate (String key , Object value , Object dependency ) {
588
+
589
+ }
590
+
591
+ }
592
+
567
593
/**
568
594
* Returns a set of settings that are required at validation time. Unless all of the dependencies are present in the settings
569
595
* object validation of setting must fail.
570
596
*/
571
- public Set <Setting <?>> getSettingsDependencies (String key ) {
597
+ public Set <SettingDependency > getSettingsDependencies (final String key ) {
572
598
return Collections .emptySet ();
573
599
}
574
600
@@ -671,13 +697,23 @@ public String toString() {
671
697
};
672
698
}
673
699
700
+ /**
701
+ * Allows an affix setting to declare a dependency on another affix setting.
702
+ */
703
+ public interface AffixSettingDependency extends SettingDependency {
704
+
705
+ @ Override
706
+ AffixSetting getSetting ();
707
+
708
+ }
709
+
674
710
public static class AffixSetting <T > extends Setting <T > {
675
711
private final AffixKey key ;
676
712
private final BiFunction <String , String , Setting <T >> delegateFactory ;
677
- private final Set <AffixSetting > dependencies ;
713
+ private final Set <AffixSettingDependency > dependencies ;
678
714
679
715
public AffixSetting (AffixKey key , Setting <T > delegate , BiFunction <String , String , Setting <T >> delegateFactory ,
680
- AffixSetting ... dependencies ) {
716
+ AffixSettingDependency ... dependencies ) {
681
717
super (key , delegate .defaultValue , delegate .parser , delegate .properties .toArray (new Property [0 ]));
682
718
this .key = key ;
683
719
this .delegateFactory = delegateFactory ;
@@ -693,12 +729,25 @@ private Stream<String> matchStream(Settings settings) {
693
729
}
694
730
695
731
@ Override
696
- public Set <Setting <?> > getSettingsDependencies (String settingsKey ) {
732
+ public Set <SettingDependency > getSettingsDependencies (String settingsKey ) {
697
733
if (dependencies .isEmpty ()) {
698
734
return Collections .emptySet ();
699
735
} else {
700
736
String namespace = key .getNamespace (settingsKey );
701
- return dependencies .stream ().map (s -> (Setting <?>)s .getConcreteSettingForNamespace (namespace )).collect (Collectors .toSet ());
737
+ return dependencies .stream ()
738
+ .map (s ->
739
+ new SettingDependency () {
740
+ @ Override
741
+ public Setting <Object > getSetting () {
742
+ return s .getSetting ().getConcreteSettingForNamespace (namespace );
743
+ }
744
+
745
+ @ Override
746
+ public void validate (final String key , final Object value , final Object dependency ) {
747
+ s .validate (key , value , dependency );
748
+ };
749
+ })
750
+ .collect (Collectors .toSet ());
702
751
}
703
752
}
704
753
@@ -1635,19 +1684,19 @@ public static <T> AffixSetting<T> prefixKeySetting(String prefix, Function<Strin
1635
1684
* out of the box unless {@link #getConcreteSetting(String)} is used to pull the updater.
1636
1685
*/
1637
1686
public static <T > AffixSetting <T > affixKeySetting (String prefix , String suffix , Function <String , Setting <T >> delegateFactory ,
1638
- AffixSetting ... dependencies ) {
1687
+ AffixSettingDependency ... dependencies ) {
1639
1688
BiFunction <String , String , Setting <T >> delegateFactoryWithNamespace = (ns , k ) -> delegateFactory .apply (k );
1640
1689
return affixKeySetting (new AffixKey (prefix , suffix ), delegateFactoryWithNamespace , dependencies );
1641
1690
}
1642
1691
1643
1692
public static <T > AffixSetting <T > affixKeySetting (String prefix , String suffix , BiFunction <String , String , Setting <T >> delegateFactory ,
1644
- AffixSetting ... dependencies ) {
1693
+ AffixSettingDependency ... dependencies ) {
1645
1694
Setting <T > delegate = delegateFactory .apply ("_na_" , "_na_" );
1646
1695
return new AffixSetting <>(new AffixKey (prefix , suffix ), delegate , delegateFactory , dependencies );
1647
1696
}
1648
1697
1649
1698
private static <T > AffixSetting <T > affixKeySetting (AffixKey key , BiFunction <String , String , Setting <T >> delegateFactory ,
1650
- AffixSetting ... dependencies ) {
1699
+ AffixSettingDependency ... dependencies ) {
1651
1700
Setting <T > delegate = delegateFactory .apply ("_na_" , "_na_" );
1652
1701
return new AffixSetting <>(key , delegate , delegateFactory , dependencies );
1653
1702
}
0 commit comments