Skip to content

Commit

Permalink
Merge pull request #54 from praxis-live/code-api-refactor
Browse files Browse the repository at this point in the history
Code API refactor
  • Loading branch information
neilcsmith-net authored Oct 4, 2023
2 parents 71c962b + 87eef62 commit af9fa58
Show file tree
Hide file tree
Showing 107 changed files with 1,736 additions and 3,486 deletions.
9 changes: 3 additions & 6 deletions praxiscore-api/src/main/java/org/praxislive/core/RootHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface RootHub extends Lookup.Provider {

/**
* Dispatch a message to another Root.
*
* <p>
* See {@link Root.Controller#submitPacket(org.praxislive.core.Packet)
*
* @param packet message to dispatch
Expand Down Expand Up @@ -78,18 +78,15 @@ public static interface ExtensionProvider {

/**
* An interface for RootHub extensions (see {@link ExtensionProvider}) to
* advertise the services they provide. Use of {@link Component#getInfo()}
* for this purpose should be considered deprecated.
*
* advertise the services they provide.
* <p>
* The Root itself must provide the advertised services. Support for root
* containers to provide services via child components is not yet supported.
*
* @see Service
*/
public static interface ServiceProvider extends Root {

// ideally this would be part of the ExtensionProvider itself, but breaks
// too many assumptions in the API
/**
* A list of the services this extension provides. This method will be
* called as the extension Root is installed in the RootHub.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.praxislive.core.Component;
import org.praxislive.core.ComponentType;
Expand Down Expand Up @@ -65,35 +64,14 @@ public interface ComponentFactory {
*/
public Stream<ComponentType> rootTypes();

@Deprecated
public default MetaData<? extends Component> getMetaData(ComponentType type) {
Supplier<Lookup> redirect = () -> componentData(type);
return new MetaData(redirect) {
};
}

@Deprecated
public default MetaData<? extends Root> getRootMetaData(ComponentType type) {
Supplier<Lookup> redirect = () -> rootData(type);
return new MetaData(redirect) {
};
}

/**
* Query the data associated with this component type.
*
* @param type component type
* @return lookup of data
*/
public default Lookup componentData(ComponentType type) {
var metadata = getMetaData(type);
if (metadata == null) {
return null;
} else if (metadata.redirect != null) {
return Lookup.EMPTY;
} else {
return metadata.getLookup();
}
return Lookup.EMPTY;
}

/**
Expand All @@ -103,14 +81,7 @@ public default Lookup componentData(ComponentType type) {
* @return lookup of data
*/
public default Lookup rootData(ComponentType type) {
var metadata = getRootMetaData(type);
if (metadata == null) {
return null;
} else if (metadata.redirect != null) {
return Lookup.EMPTY;
} else {
return metadata.getLookup();
}
return Lookup.EMPTY;
}

/**
Expand Down Expand Up @@ -139,21 +110,6 @@ public default Root createRoot(ComponentType type) throws ComponentInstantiation
throw new ComponentInstantiationException();
}

@Deprecated
public default Root createRootComponent(ComponentType type) throws ComponentInstantiationException {
return createRoot(type);
}

@Deprecated
public default Class<? extends ComponentFactoryService> getFactoryService() {
return ComponentFactoryService.class;
}

@Deprecated
public default Class<? extends RootFactoryService> getRootFactoryService() {
return RootFactoryService.class;
}

/**
* Optional service to redirect to for component instantiation. The control
* on the service should follow the same shape as the default
Expand All @@ -164,12 +120,7 @@ public default Class<? extends RootFactoryService> getRootFactoryService() {
* @return optional service redirect
*/
public default Optional<Redirect> componentRedirect() {
var service = getFactoryService();
if (service == null || ComponentFactoryService.class.equals(service)) {
return Optional.empty();
} else {
return Optional.of(new Redirect(service, ComponentFactoryService.NEW_INSTANCE));
}
return Optional.empty();
}

/**
Expand All @@ -182,104 +133,40 @@ public default Optional<Redirect> componentRedirect() {
* @return optional service redirect
*/
public default Optional<Redirect> rootRedirect() {
var service = getRootFactoryService();
if (service == null || RootFactoryService.class.equals(service)) {
return Optional.empty();
} else {
return Optional.of(new Redirect(service, RootFactoryService.NEW_ROOT_INSTANCE));
}
}

@Deprecated
public static abstract class MetaData<T> {

private final Supplier<Lookup> redirect;

public MetaData() {
this.redirect = null;
}

private MetaData(Supplier<Lookup> redirect) {
this.redirect = redirect;
}

public boolean isDeprecated() {
return false;
}

public Optional<ComponentType> findReplacement() {
return Optional.empty();
}

public Lookup getLookup() {
return redirect != null ? redirect.get() : Lookup.EMPTY;
}

return Optional.empty();
}

/**
* A factory service redirect. See {@link #componentRedirect()} and
* {@link #rootRedirect()}.
*/
public static final class Redirect {
public record Redirect(Class<? extends Service> service, String control) {

private final Class<? extends Service> service;
private final String control;
}

/**
* Construct a redirect to the provided service and control.
*
* @param service service type to redirect to
* @param control control on service to redirect to
*/
public Redirect(Class<? extends Service> service, String control) {
this.service = Objects.requireNonNull(service);
this.control = Objects.requireNonNull(control);
}
/**
* Mark a component deprecated (with optional replacement type). An instance
* should be added to the metadata lookup associated with the component
* type.
*/
public record Deprecated(Optional<ComponentType> replacement) {

/**
* Query the service to redirect to.
*
* @return service
* Deprecated without replacement.
*/
public Class<? extends Service> service() {
return service;
public Deprecated() {
this(Optional.empty());
}

/**
* Query the control on the service to redirect to.
* Deprecated with replacement type.
*
* @return control id
* @param replacement replacement type
*/
public String control() {
return control;
}

@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + Objects.hashCode(this.service);
hash = 97 * hash + Objects.hashCode(this.control);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Redirect other = (Redirect) obj;
if (!Objects.equals(this.control, other.control)) {
return false;
}
return Objects.equals(this.service, other.service);
public Deprecated(ComponentType replacement) {
this(Optional.of(replacement));
}

}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2023 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code 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 Lesser General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License version 3
* along with this work; if not, see http://www.gnu.org/licenses/
*
*
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.audio.code;

import java.util.List;
import java.util.stream.Stream;
import org.praxislive.code.CodeFactory;
import org.praxislive.code.CodeUtils;

/**
* Audio code utility functions.
*/
public class AudioCode {

private static final List<String> DEFAULT_IMPORTS
= Stream.concat(CodeUtils.defaultImports().stream(),
Stream.of("org.jaudiolibs.pipes.*",
"org.jaudiolibs.pipes.units.*",
"org.praxislive.audio.code.userapi.*",
"static org.praxislive.audio.code.userapi.AudioConstants.*"))
.toList();

private static final CodeFactory.Base<AudioCodeDelegate> BASE
= CodeFactory.base(AudioCodeDelegate.class,
DEFAULT_IMPORTS,
(task, delegate) -> new AudioCodeContext(new AudioCodeConnector(task, delegate)));

private AudioCode() {

}

/**
* Access {@link CodeFactory.Base} for {@link AudioCodeDelegate}.
*
* @return code factory base for AudioCodeDelegate.
*/
public static CodeFactory.Base<AudioCodeDelegate> base() {
return BASE;
}
}
Loading

0 comments on commit af9fa58

Please sign in to comment.