net.incrementalism.tooter.User.java Source code

Java tutorial

Introduction

Here is the source code for net.incrementalism.tooter.User.java

Source

/*
 * Copyright 2010 Tim Moore
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.incrementalism.tooter;

import org.apache.commons.codec.binary.Base64;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.CopyOnWriteArraySet;

import static java.util.Collections.unmodifiableList;

public final class User implements Serializable {
    private final String userName;
    private final Collection<String> followers = new CopyOnWriteArraySet<String>();
    private final String hashedPassword;

    private User(Builder builder) {
        this.userName = builder.userName;
        this.hashedPassword = builder.hashedPassword;
    }

    public String getUserName() {
        return userName;
    }

    public static User.Builder builder() {
        return new User.Builder();
    }

    public void addFollower(String followerUserName) {
        followers.add(followerUserName);
    }

    public boolean hasFollower(String userName) {
        return followers.contains(userName);
    }

    public Iterable<String> getFollowers() {
        return unmodifiableList(new LinkedList<String>(followers));
    }

    public boolean hasPassword(String password) {
        return hashedPassword.equals(hash(password));
    }

    static class Builder {
        String userName;
        String hashedPassword;

        private Builder() {
            super();
        }

        public Builder userName(String userName) {
            this.userName = userName;
            return this;
        }

        public Builder email(String email) {
            return this;
        }

        public Builder password(String password) {
            this.hashedPassword = hash(password);
            return this;
        }

        public User build() {
            return new User(this);
        }
    }

    private static String hash(String password) {
        // NOTE: Insecure! Don't use this in a real application
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(password.getBytes("UTF-8"));
            return Base64.encodeBase64String(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError("No SHA");
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError("No UTF-8");
        }
    }
}