package com.sorakasugano.pasteboard;

import java.util.*;
import redis.clients.jedis.*;

class StarsCounter extends Reader<Long> {
    @Override
    public Long call(Jedis jedis) throws Exception {
        return jedis.scard(type + ":" + id + ":star-list");
    }
    @Override
    public Map<String, Boolean> keys() {
        Map<String, Boolean> keys = super.keys();
        keys.put(type + ":" + id + ":star-list", false);
        return keys;
    }
}

public class DBUtils {
    public static Long countStars(String id) throws Exception {
        StarsCounter counter = new StarsCounter();
        counter.type = "code";
        counter.id = id;
        return Adapter.invoke(counter);
    }
}

@@ -51,16 +51,45 @@
 util.*;%0A
+import javax.servlet.http.*;%0A
 import r
@@ -482,237 +482,2627 @@
 %0A%7D%0A%0A
-public class DBUtils %7B%0A    public static Long countStars(String id) throws Exception %7B%0A        StarsCounter counter = new StarsCounter();%0A        counter.type = %22code%22;%0A        counter.id = id;%0A        return Adapter.invoke(count
+abstract class StarHelper extends Writer %7B%0A    public String user = null;%0A    @Override%0A    public Map%3CString, Boolean%3E keys() %7B%0A        Map%3CString, Boolean%3E keys = super.keys();%0A        keys.put(type + %22:%22 + id + %22:star-list%22, true);%0A        keys.put(%22user:%22 + user + %22:star-list%22, true);%0A        return keys;%0A    %7D%0A%7D%0A%0Apublic class DBUtils %7B%0A    public static Map%3CString, String%3E self(HttpServletRequest req) throws Exception %7B%0A        String token = null;%0A        Cookie%5B%5D cookies = req.getCookies();%0A        if (cookies != null) %7B%0A            for (Cookie cookie : cookies) %7B%0A                if (cookie.getName().equals(%22token%22)) %7B%0A                    token = cookie.getValue();%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (token != null) %7B%0A            Reader%3CString%3E reader = new Reader%3CString%3E() %7B%0A                @Override%0A                public String call(Jedis jedis) throws Exception %7B%0A                    return jedis.get(type + %22:%22 + id);%0A                %7D%0A            %7D;%0A            reader.type = %22token%22;%0A            reader.id = token;%0A            String id = Adapter.invoke(reader);%0A            if (id != null) %7B%0A                Getter getter = new Getter();%0A                getter.type = %22user%22;%0A                getter.id = id;%0A                return Adapter.invoke(getter);%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A    public static Long countStars(String id) throws Exception %7B%0A        StarsCounter counter = new StarsCounter();%0A        counter.type = %22code%22;%0A        counter.id = id;%0A        return Adapter.invoke(counter);%0A    %7D%0A    public static void star(String cid, String uid) throws Exception %7B%0A        StarHelper helper = new StarHelper() %7B%0A            @Override%0A            public Object call(Jedis jedis) throws Exception %7B%0A                jedis.sadd(type + %22:%22 + id + %22:star-list%22, user);%0A                jedis.zadd(%22user:%22 + user + %22:star-list%22, new Date().getTime(), id);%0A                return null;%0A            %7D%0A        %7D;%0A        helper.type = %22code%22;%0A        helper.id = cid;%0A        helper.user = uid;%0A        Adapter.invoke(helper);%0A    %7D%0A    public static void unstar(String cid, String uid) throws Exception %7B%0A        StarHelper helper = new StarHelper() %7B%0A            @Override%0A            public Object call(Jedis jedis) throws Exception %7B%0A                jedis.srem(type + %22:%22 + id + %22:star-list%22, user);%0A                jedis.zrem(%22user:%22 + user + %22:star-list%22, id);%0A                return null;%0A            %7D%0A        %7D;%0A        helper.type = %22code%22;%0A        helper.id = cid;%0A        helper.user = uid;%0A        Adapter.invoke(help
 er);

implement database utils