Skip to content

Commit

Permalink
Add optional meta control to ComponentProtocol and tidy up protocols.
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcsmith-net committed Oct 13, 2023
1 parent 2e73a40 commit fd35484
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
2 changes: 1 addition & 1 deletion praxiscore-api/src/main/java/org/praxislive/core/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public static abstract class ArgumentInfoBuilder<T extends ArgumentInfoBuilder<T
* @return this
*/
@SuppressWarnings("unchecked")
public T property(String key, Value value) {
public T property(String key, Object value) {
if (ArgumentInfo.KEY_TYPE.equals(key)) {
throw new IllegalArgumentException("Reserved key");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,57 @@
package org.praxislive.core.protocols;

import java.util.stream.Stream;
import org.praxislive.core.ArgumentInfo;
import org.praxislive.core.Component;
import org.praxislive.core.ComponentInfo;
import org.praxislive.core.ControlInfo;
import org.praxislive.core.Info;
import org.praxislive.core.Protocol;
import org.praxislive.core.types.PMap;

/**
* Basic component protocol, providing access to the component info.
* Basic component protocol, providing access to the component info, and
* optional support for attaching metadata.
*/
public class ComponentProtocol implements Protocol {

@Deprecated
public final static ComponentProtocol INSTANCE = new ComponentProtocol();
public final class ComponentProtocol implements Protocol {

/**
* Name of the info control.
*/
public final static String INFO = "info";
public static final String INFO = "info";

/**
* Name of the optional meta control.
*/
public static final String META = "meta";

/**
* Control info for the info control. A read-only property that returns the
* component info. The response to calling this control should be the same
* as calling {@link Component#getInfo}.
*/
public final static ControlInfo INFO_INFO
= ControlInfo.createReadOnlyPropertyInfo(
ComponentInfo.info(),
null);
public static final ControlInfo INFO_INFO
= Info.control(c -> c.readOnlyProperty().output(ComponentInfo.class));

/**
* Control info for the optional control. This control allows for arbitrary
* metadata to be attached to a component. The control works the same as a
* map property, except that the optional PMap input is replace merged with
* the existing value. This allows callers to replace or delete only the
* keys they are interested in.
* <p>
* See also {@link PMap#REPLACE}.
*/
public static final ControlInfo META_INFO
= Info.control(c -> c.function()
.inputs(a -> a.type(PMap.class).property(ArgumentInfo.KEY_OPTIONAL, true))
.outputs(a -> a.type(PMap.class)));

/**
* A component info for this protocol. Can be used with
* {@link Info.ComponentInfoBuilder#merge(org.praxislive.core.ComponentInfo)}.
*/
public final static ComponentInfo API_INFO = Info.component(cmp -> cmp
public static final ComponentInfo API_INFO = Info.component(cmp -> cmp
.protocol(ComponentProtocol.class)
.control(INFO, INFO_INFO)
);
Expand All @@ -65,12 +82,21 @@ public Stream<String> controls() {
return Stream.of(INFO);
}

@Override
public Stream<String> optionalControls() {
return Stream.of(META);
}

@Override
public ControlInfo getControlInfo(String control) {
if (INFO.equals(control)) {
return INFO_INFO;
}
throw new IllegalArgumentException();
return switch (control) {
case INFO ->
INFO_INFO;
case META ->
META_INFO;
default ->
throw new IllegalArgumentException();
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
* A container protocol that allows for calls to add / remove child components,
* and connect / disconnect their ports.
*/
public class ContainerProtocol implements Protocol {

@Deprecated
public final static ContainerProtocol INSTANCE = new ContainerProtocol();
public final class ContainerProtocol implements Protocol {

/**
* Name of the add-child control.
Expand Down Expand Up @@ -187,23 +184,24 @@ public Stream<String> optionalControls() {

@Override
public ControlInfo getControlInfo(String control) {
switch (control) {
case ADD_CHILD:
return ADD_CHILD_INFO;
case REMOVE_CHILD:
return REMOVE_CHILD_INFO;
case CHILDREN:
return CHILDREN_INFO;
case CONNECT:
return CONNECT_INFO;
case DISCONNECT:
return DISCONNECT_INFO;
case CONNECTIONS:
return CONNECTIONS_INFO;
case SUPPORTED_TYPES:
return SUPPORTED_TYPES_INFO;
}
throw new IllegalArgumentException();
return switch (control) {
case ADD_CHILD ->
ADD_CHILD_INFO;
case REMOVE_CHILD ->
REMOVE_CHILD_INFO;
case CHILDREN ->
CHILDREN_INFO;
case CONNECT ->
CONNECT_INFO;
case DISCONNECT ->
DISCONNECT_INFO;
case CONNECTIONS ->
CONNECTIONS_INFO;
case SUPPORTED_TYPES ->
SUPPORTED_TYPES_INFO;
default ->
throw new IllegalArgumentException();
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
*/
public class StartableProtocol implements Protocol {

@Deprecated
public final static StartableProtocol INSTANCE = new StartableProtocol();

/**
* Name of the start control.
*/
Expand Down Expand Up @@ -94,14 +91,15 @@ public Stream<String> controls() {

@Override
public ControlInfo getControlInfo(String control) {
switch (control) {
case START:
return START_INFO;
case STOP:
return STOP_INFO;
case IS_RUNNING:
return IS_RUNNING_INFO;
}
throw new IllegalArgumentException();
return switch (control) {
case START ->
START_INFO;
case STOP ->
STOP_INFO;
case IS_RUNNING ->
IS_RUNNING_INFO;
default ->
throw new IllegalArgumentException();
};
}
}

0 comments on commit fd35484

Please sign in to comment.