Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SourceChanger?! #224

Open
tischi opened this issue May 18, 2021 · 7 comments
Open

Implement SourceChanger?! #224

tischi opened this issue May 18, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@tischi
Copy link
Collaborator

tischi commented May 18, 2021

@NicoKiaru

Do you think this could be useful? If it works I think it could avoid quite some code duplication:

public class SourceChanger< T > implements Supplier< SourceAndConverter< ? > >
{
    private final SourceAndConverter< ? > sourceAndConverter;
    private final Function< Source< ? >, Source< ? > > sourceConverter;

    public SourceChanger( SourceAndConverter< T > sourceAndConverter, Function< Source< ? >, Source< ? > > sourceConverter )
    {
        this.sourceAndConverter = sourceAndConverter;
        this.sourceConverter = sourceConverter;
    }

    public SourceAndConverter< ? > get()
    {
        Source< ? > src = sourceConverter.apply( sourceAndConverter.getSpimSource() );

        if ( sourceAndConverter.asVolatile() != null )
        {
            Source< ? > vsrc = sourceConverter.apply( sourceAndConverter.asVolatile().getSpimSource() );

            SourceAndConverter< ? > vsac = new SourceAndConverter( vsrc, sourceAndConverter.asVolatile().getConverter(), sourceAndConverter.asVolatile() );

            final SourceAndConverter< ? > sourceAndConverter = new SourceAndConverter( src, this.sourceAndConverter.getConverter(), vsac );
            
            return sourceAndConverter;
        }
        else
        {
            final SourceAndConverter< ? > sourceAndConverter = new SourceAndConverter( src, this.sourceAndConverter.getConverter() );
            
            return sourceAndConverter;
        }
    }
}

The Function< Source< ? >, Source< ? > > sourceConverter would for example return a TransformedSource or a WarpedSource, given an input Source.

For this one needs to create a SourceTransformer implements Function< Source< ? >, Source< ? > > and a SourceWarper implements Function< Source< ? >, Source< ? > >. But we needed this anyway (see e.g. SourceResampler). The advantage would be that with above code the SourceResampler does not need to deal with the generic is of creating also the Volatile versions.

@tischi
Copy link
Collaborator Author

tischi commented May 18, 2021

I played with it and it is working.

Here is an improved version of the class:

public class SourceChanger< T > implements Function< SourceAndConverter< ? >,  SourceAndConverter< ? > >
{
    private final Function< Source< ? >, Source< ? > > sourceConverter;

    public SourceChanger( Function< Source< ? >, Source< ? > > sourceConverter )
    {
        this.sourceConverter = sourceConverter;
    }

    @Override
    public SourceAndConverter< ? > apply( SourceAndConverter< ? > inputSourceAndConverter )
    {
        Source< ? > outputSource = sourceConverter.apply( inputSourceAndConverter.getSpimSource() );

        if ( inputSourceAndConverter.asVolatile() != null )
        {
            final Source< ? > volatileOutputSource = sourceConverter.apply( inputSourceAndConverter.asVolatile().getSpimSource() );

            final SourceAndConverter< ? > volatileOutputSourceAndConverter = new SourceAndConverter( volatileOutputSource, inputSourceAndConverter.asVolatile().getConverter() );

            final SourceAndConverter< ? > outputSourceAndConverter = new SourceAndConverter( outputSource, inputSourceAndConverter.getConverter(), volatileOutputSourceAndConverter );

            return outputSourceAndConverter;
        }
        else
        {
            return new SourceAndConverter( outputSource, inputSourceAndConverter.getConverter() );
        }
    }
}

One can use this now even with a lambda, for example like this:

final SourceAndConverter< T > croppedSourceAndConverter = new SourceChanger( ( Function< Source< ? >, Source< ? > > ) source -> new CroppedSource( source, "someName", new FinalRealInterval( min, max ), shiftToOrigin ) ).apply( sourceAndConverter );

That means one only has to implement the class the converts the source and the rest can be done by the SourceChanger.

@NicoKiaru
Copy link
Collaborator

Looks good! Do you think it's better as an action or it could fit as a static function in sourceandconverter helper.

Also : regarding the converter, I uses the clone converter function instead of reusing the same converter. Do you have a good reason not to clone it ? Should we make this optional (clone or reuse?)

@tischi
Copy link
Collaborator Author

tischi commented May 18, 2021

Do you think it's better as an action or it could fit as a static function in sourceandconverter helper.

I think it should be a class, because otherwise we cannot implement the apply functional interface which can be handy. In addition, it is a pendant to the ConverterChanger which also is a class.
If you agree to make it a class, where should I put it?

Should we make this optional (clone or reuse?)

Yes, should be optional, one needs both I think. I can amend the code to enable cloning.

@NicoKiaru
Copy link
Collaborator

go for it, with optional converter cloning ! 👍

@tischi
Copy link
Collaborator Author

tischi commented May 18, 2021

But in which package should I put it?

@NicoKiaru
Copy link
Collaborator

Here https://github.com/bigdataviewer/bigdataviewer-playground/tree/master/src/main/java/sc/fiji/bdvpg ? Within a new source package ?

@tischi
Copy link
Collaborator Author

tischi commented May 20, 2021

I'll be on holidays from tomorrow for one week 🌴 ⛰️
I'll do this once I am back!

@NicoKiaru NicoKiaru added the enhancement New feature or request label Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants