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

com.uwetrottmann.trakt5.entities.UserSlug Maven / Gradle / Ivy

There is a newer version: 6.16.0
Show newest version
package com.uwetrottmann.trakt5.entities;

public class UserSlug {

    /**
     * Special user slug for the current user (determined by auth data).
     */
    public static final UserSlug ME = new UserSlug("me");

    private String userSlug;

    /**
     * User slug to pass to the API, performs some simple null and empty checks.
     *
     * @param userSlug A user slug as returned by the trakt API: {@link User.UserIds}.
     * @see #ME
     */
    public UserSlug(String userSlug) {
        if (userSlug == null) {
            throw new IllegalArgumentException("trakt user slug can not be null.");
        }
        userSlug = userSlug.trim();
        if (userSlug.length() == 0) {
            throw new IllegalArgumentException("trakt user slug can not be empty.");
        }
        this.userSlug = userSlug;
    }

    /**
     * Encodes the username returned from trakt so it is API compatible (currently replaces "." and spaces with "-").
     */
    public static UserSlug fromUsername(String username) {
        if (username == null) {
            throw new IllegalArgumentException("trakt username can not be null.");
        }
        // trakt encodes some special chars in usernames
        // - points "." as a dash "-"
        // - spaces " " as a dash "-"
        // - multiple dashes are reduced to one
        String slug = username.replace(".", "-").replace(" ", "-").replaceAll("(-)+", "-");
        return new UserSlug(slug);
    }

    @Override
    public String toString() {
        return userSlug;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy