package com.veyndan.redditclient.post.mutator;

import com.google.common.collect.ImmutableList;
import com.veyndan.redditclient.post.model.Post;

import java.util.List;

import rx.functions.Action1;

public final class Mutators {

    /**
     * All available mutator factories.
     * <p>
     * Note that the order of the mutator factories is the order in which the post will be mutated.
     */
    private static final List<MutatorFactory> MUTATOR_FACTORIES = ImmutableList.of(
            TextMutatorFactory.create(),
            LinkImageMutatorFactory.create(),
            LinkMutatorFactory.create(),
            ImgurMutatorFactory.create(),
            TwitterMutatorFactory.create(),
            XkcdMutatorFactory.create()
    );

    /**
     * Mutate a list of posts by all the available mutator factories.
     */
    public Action1<Post> mutate() {
        return post -> {
            for (final MutatorFactory mutatorFactory : MUTATOR_FACTORIES) {
                if (mutatorFactory.applicable(post)) mutatorFactory.mutate(post);
            }
        };
    }
}

@@ -377,16 +377,880 @@
 l be
- mutated
+%0A     * checked for applicability of mutation.%0A     * %3Cp%3E%0A     * Example if the %7B@link TwitterMutatorFactory%7D was set%0A     * after %7B@link LinkMutatorFactory%7D then the post would see if the %7B@link LinkMutatorFactory%7D is%0A     * applicable to mutate the post first, and if %7B@code false%7D, then %7B@link TwitterMutatorFactory%7D%0A     * would check if it is applicable. In this case the %7B@link TwitterMutatorFactory%7D would never%0A     * be checked for applicability or be applicable, as if the post contains a link, then%0A     * %7B@link LinkMutatorFactory%7D would be applicable, then mutation would stop. If%0A     * %7B@link LinkMutatorFactory%7D isn't applicable, then the post doesn't have a link, so%0A     * %7B@link TwitterMutatorFactory%7D would never be applicable. Obviously this means that%0A     * %7B@link LinkMutatorFactory%7D should occur %3Cb%3E%3Cem%3Eafter%3C/em%3E%3C/b%3E %7B@link TwitterMutatorFactory%7D
 .%0A  
@@ -1356,11 +1356,14 @@
    T
-ext
+witter
 Muta
@@ -1399,58 +1399,12 @@
     
-LinkImageMutatorFactory.create(),%0A            Link
+Xkcd
 Muta
@@ -1471,38 +1471,81 @@
 ),%0A            T
-witter
+extMutatorFactory.create(),%0A            LinkImage
 MutatorFactory.c
@@ -1557,36 +1557,36 @@
 (),%0A            
-Xkcd
+Link
 MutatorFactory.c
@@ -1646,43 +1646,64 @@
  by 
-all the avail
+the first mutator which is applic
 able 
+to 
 mutat
-or factories
+e the post
 .%0A  
@@ -1897,16 +1897,38 @@
 e(post))
+ %7B%0A                   
  mutator
@@ -1949,16 +1949,62 @@
 (post);%0A
+                    return;%0A                %7D%0A
         

Stop mutating a post after the first applicable mutation