Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:59:23+00:00 2026-05-27T19:59:23+00:00

I’ve been struggling with the following problem. I have a series of function objects,

  • 0

I’ve been struggling with the following problem. I have a series of function objects, each with it’s own input and output types defined via generic type arguments in java. I would like to arrange these in a chain so that raw data is input to the first function, transformed to the into the output type, which is the input type of the next object, and so on. of course this would be trivial to hard-code, but i’d like to have the code be pluggable to new function objects. if i just leave out type arguments (only the final output type), this is how things look:

    public T process() {
        Iterator<Context> it = source.provideData();
        for(Pipe pipe : pipeline) {
            it = pipe.processIterator(it);
        }
        return sink.next(it);
    }

here an iterator over the data is passed between function objects, and context should be Context. is there a way to keep the following kind of pipe pluggable and still maintain type safety?

edit:
for clarity, i have a series of function objects, pipes. each takes as input a particular type and outputs another type. (actually an iterators over these types) these will be chained together, eg, Pipe<A,B> -> Pipe<B,C> -> Pipe<C,D> -> ..., so that the output of one pipe is the input type for the next pipe. There is also a source here that outputs an iterator of type A, and a sink that would accept type (the output of the past pipe). does this make things clearer? The question is, because there is critical dependence on the compatibility of input and output types, is there a way to ensure this?

I am starting to think that on insert of the function objects into the pipeline may be the best time to ensure type safety, but i’m not sure how to do this.

edit 2:
i have a adder method for the function objects that currently looks like below:

public void addPipe(Pipe<?,?> pipe) {
    pipeline.add(pipe);
}

i’d like to check if the first type parameter is the same as the “end” of the current pipe, and throw an exception if not? i dont think there is a good way to get compile time safety here. the “end” of the current pipe can then be set to the second type param of the input pipe. I can’t think of how to do this with generics, and passing around the class information seems pretty hideous.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:59:24+00:00Added an answer on May 27, 2026 at 7:59 pm

    Here’s a way to do it. The run method is not typesafe, but given that the only way to append a pipe is to do it in a type-safe way, the whole chain is type-safe.

    public class Chain<S, T> {
        private List<Pipe<?, ?>> pipes;
    
        private Chain() {
        }
    
        public static <K, L> Chain<K, L> start(Pipe<K, L> pipe) {
            Chain<K, L> chain = new Chain<K, L>();
            chain.pipes = Collections.<Pipe<?, ?>>singletonList(pipe);;
            return chain;
        }
    
        public <V> Chain<S, V> append(Pipe<T, V> pipe) {
            Chain<S, V> chain = new Chain<S, V>();
            chain.pipes = new ArrayList<Pipe<?, ?>>(pipes);
            chain.pipes.add(pipe);
            return chain;
        }
    
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public T run(S s) {
            Object source = s;
            Object target = null;
            for (Pipe p : pipes) {
                target = p.transform(source);
                source = target;
            }
            return (T) target;
        }
    
        public static void main(String[] args) {
            Pipe<String, Integer> pipe1 = new Pipe<String, Integer>() {
                @Override
                public Integer transform(String s) {
                    return Integer.valueOf(s);
                }
            };
            Pipe<Integer, Long> pipe2 = new Pipe<Integer, Long>() {
                @Override
                public Long transform(Integer s) {
                    return s.longValue();
                }
            };
            Pipe<Long, BigInteger> pipe3 = new Pipe<Long, BigInteger>() {
                @Override
                public BigInteger transform(Long s) {
                    return new BigInteger(s.toString());
                }
            };
            Chain<String, BigInteger> chain = Chain.start(pipe1).append(pipe2).append(pipe3);
            BigInteger result = chain.run("12");
            System.out.println(result);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.