All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.marble.Parser Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package rx.marble;

import rx.Notification;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Alexandre Victoor on 05/06/2016.
 */
public class Parser {


    public static  List> parseMarbles(String marbles,
                                                                   Map values,
                                                                   Exception errorValue,
                                                                   long frameTimeFactor,
                                                                   boolean materializeInnerObservables) {

        if (marbles.indexOf('!') != -1) {
            throw new IllegalArgumentException("Conventional marble diagrams cannot have the unsubscription marker '!'");
        }

        int len = marbles.length();
        List> testMessages = new ArrayList<>();
        int subIndex = marbles.indexOf('^');
        long frameOffset = subIndex == -1 ? 0 : (subIndex * -frameTimeFactor);

        long groupStart = -1;

        for (int i = 0; i < len; i++) {
            long frame = i * frameTimeFactor + frameOffset;
            Notification notification = null;
            char c = marbles.charAt(i);
            switch (c) {
                case '-':
                case ' ':
                    break;
                case '(':
                    groupStart = frame;
                    break;
                case ')':
                    groupStart = -1;
                    break;
                case '|':
                    notification = Notification.createOnCompleted();
                    break;
                case '^':
                    break;
                case '#':
                    notification = Notification.createOnError(errorValue);
                    break;
                default:
                    T value;
                    if (values == null) {
                        value = (T)String.valueOf(c);
                    } else {
                        value = values.get(String.valueOf(c));
                        if (materializeInnerObservables && value instanceof ColdObservable) {
                            value = (T)((ColdObservable)value).getMessages();
                        }
                    }
                    notification = Notification.createOnNext(value);
                    break;
            }

            if (notification != null) {
                long messageFrame = groupStart > -1 ? groupStart : frame;
                testMessages.add(new Recorded<>(messageFrame, notification));
            }
        }
        return testMessages;
    }

    public static  List> parseMarbles(String marbles, Map values, long frameTimeFactor) {
        return parseMarbles(marbles, values, null, frameTimeFactor);
    }

    public static  List> parseMarbles(String marbles, Map values, Exception errorValue, long frameTimeFactor) {
        return parseMarbles(marbles, values, errorValue, frameTimeFactor, false);
    }

    public static List> parseMarbles(String marbles, long frameTimeFactor) {
        return parseMarbles(marbles, null, frameTimeFactor);
    }

    public static SubscriptionLog parseMarblesAsSubscriptions(String marbles, long frameTimeFactor) {
        int len = marbles.length();
        long groupStart = -1;
        long subscriptionFrame = Long.MAX_VALUE;
        long unsubscriptionFrame = Long.MAX_VALUE;

        for (int i = 0; i < len; i++) {
            long frame = i * frameTimeFactor;
            char c = marbles.charAt(i);
            switch (c) {
                case '-':
                case ' ':
                    break;
                case '(':
                    groupStart = frame;
                    break;
                case ')':
                    groupStart = -1;
                    break;
                case '^':
                    if (subscriptionFrame != Long.MAX_VALUE) {
                        throw new IllegalArgumentException("Found a second subscription point \'^\' in a " +
                                "subscription marble diagram. There can only be one.");
                    }
                    subscriptionFrame = groupStart > -1 ? groupStart : frame;
                    break;
                case '!':
                    if (unsubscriptionFrame != Long.MAX_VALUE) {
                        throw new IllegalArgumentException("Found a second subscription point \'^\' in a " +
                                "subscription marble diagram. There can only be one.");
                    }
                    unsubscriptionFrame = groupStart > -1 ? groupStart : frame;
                    break;
                default:
                    throw new IllegalArgumentException("There can only be \'^\' and \'!\' markers in a " +
                            "subscription marble diagram. Found instead \'' + c + '\'.");
            }
        }

        if (unsubscriptionFrame < 0) {
            return new SubscriptionLog(subscriptionFrame);
        }
        return new SubscriptionLog(subscriptionFrame, unsubscriptionFrame);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy