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

nl.vpro.domain.DomainObjects Maven / Gradle / Ivy

Go to download

Several domains like 'media', pages' and 'subtitles' in the POMS system share some common properties which are collected here

There is a newer version: 8.3.1
Show newest version
package nl.vpro.domain;

import java.io.File;
import java.nio.file.Path;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.function.Consumer;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
 * @author Michiel Meeuwissen
 * @since 5.7
 */
public class DomainObjects {

    private DomainObjects() {
    }

    public static String getCanonicalFilePath(@NonNull DomainObject domain) {
        String id = String.valueOf(domain.getId());
        StringBuilder path = new StringBuilder(id.length() * 2);
        appendCanonicalFilePath(domain, path);
        return path.toString();
    }


    public static void appendCanonicalFilePath(@NonNull DomainObject domain, @NonNull StringBuilder builder) {
        appendCanonicalFilePath(domain.getId(), builder);
    }

    public static void appendCanonicalFilePath(long i, @NonNull StringBuilder builder) {
        appendCanonicalFilePath(String.valueOf(i), builder);
    }

    public static void appendCanonicalFilePath(@NonNull final String id, @NonNull StringBuilder builder) {
        appendCanonicalFilePath(id, (p) -> builder.append(p).append(File.separator));
    }

    public static Path appendCanonicalFilePath(long id, @NonNull Path path) {
        return appendCanonicalFilePath(String.valueOf(id), path);
    }

    public static Path appendCanonicalFilePath(@NonNull final String id, @NonNull Path path) {
        final Path[] p = new Path[]{path};
        appendCanonicalFilePath(id, s -> p[0] = p[0].resolve(s));
        return p[0];
    }

    private  static void appendCanonicalFilePath(@NonNull final String id, Consumer appender) {

        final CharacterIterator it = new StringCharacterIterator(id);
        final StringBuilder builder = new StringBuilder();
        for(char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
            builder.append(c);
            // Second digit
            c = it.next();
            if(c != CharacterIterator.DONE) {
                builder.append(c);
            }
            appender.accept(builder.toString());
            builder.setLength(0);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy