001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.model;
020
021import org.shredzone.flattr4j.connector.FlattrObject;
022
023/**
024 * A single Category that is available for Things. Two {@link Category} are considered
025 * equal if they contain the same id.
026 *
027 * @author Richard "Shred" Körber
028 */
029public class Category extends Resource implements CategoryId {
030    private static final long serialVersionUID = 6749493295567461888L;
031
032    /**
033     * Returns a {@link CategoryId} for the given Category id.
034     *
035     * @param id
036     *            Category id
037     * @return A {@link CategoryId} object for this id
038     */
039    public static CategoryId withId(final String id) {
040        return new CategoryId() {
041            @Override
042            public String getCategoryId() {
043                return id;
044            }
045        };
046    }
047
048    public Category(FlattrObject data) {
049        super(data);
050    }
051
052    /**
053     * Category id to be used with Flattr.
054     */
055    @Override
056    public String getCategoryId() {
057        return data.get("id");
058    }
059
060    /**
061     * Category name to be used for humans.
062     */
063    public String getName() {
064        return data.get("text");
065    }
066
067    @Override
068    public boolean equals(Object obj) {
069        String pk = getCategoryId();
070
071        if (pk == null || obj == null || !(obj instanceof Category)) {
072            return false;
073        }
074
075        return pk.equals(((Category) obj).getCategoryId());
076    }
077
078    @Override
079    public int hashCode() {
080        String pk = getCategoryId();
081        return (pk != null ? pk.hashCode() : 0);
082    }
083
084}