class LocalFirstLoadBalance<T> extends AbstractLoadBalance<T> {
    public static final int MAX_REFERER_COUNT = 10;

    public static long ipToLong(final String addr) {
        final String[] addressBytes = addr.split("\\.");
        int length = addressBytes.length;
        if (length < 3) {
            return 0;
        }
        long ip = 0;
        try {
            for (int i = 0; i < 4; i++) {
                ip <<= 8;
                ip |= Integer.parseInt(addressBytes[i]);
            }
        } catch (Exception e) {
            LoggerUtil.warn("Warn ipToInt addr is wrong: addr=" + addr);
        }

        return ip;
    }

    @Override
    protected Referer<T> doSelect(Request request) {
        List<Referer<T>> referers = getReferers();

        List<Referer<T>> localReferers = searchLocalReferer(referers, NetUtils.getLocalAddress().getHostAddress());

        if (!localReferers.isEmpty()) {
            referers = localReferers;
        }

        int refererSize = referers.size();
        Referer<T> referer = null;

        for (int i = 0; i < refererSize; i++) {
            Referer<T> temp = referers.get(i % refererSize);

            if (!temp.isAvailable()) {
                continue;
            }

            if (referer == null) {
                referer = temp;
            } else {
                if (compare(referer, temp) > 0) {
                    referer = temp;
                }
            }
        }

        return referer;
    }

    @Override
    protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder) {<FILL_FUNCTION_BODY>}

    private List<Referer<T>> searchLocalReferer(List<Referer<T>> referers, String localhost) {
        List<Referer<T>> localReferers = new ArrayList<Referer<T>>();
        long local = ipToLong(localhost);
        for (Referer<T> referer : referers) {
            long tmp = ipToLong(referer.getUrl().getHost());
            if (local != 0 && local == tmp) {
                if (referer.isAvailable()) {
                    localReferers.add(referer);
                }
            }
        }

        return localReferers;
    }

    private int compare(Referer<T> referer1, Referer<T> referer2) {
        return referer1.activeRefererCount() - referer2.activeRefererCount();
    }

    static class LowActivePriorityComparator<T> implements Comparator<Referer<T>> {
        @Override
        public int compare(Referer<T> referer1, Referer<T> referer2) {
            return referer1.activeRefererCount() - referer2.activeRefererCount();
        }
    }
}

        List<Referer<T>> referers = getReferers();

        List<Referer<T>> localReferers = searchLocalReferer(referers, NetUtils.getLocalAddress().getHostAddress());

        if (!localReferers.isEmpty()) {
            Collections.sort(localReferers, new LowActivePriorityComparator<T>());
            refersHolder.addAll(localReferers);
        }

        int refererSize = referers.size();
        int startIndex = ThreadLocalRandom.current().nextInt(refererSize);
        int currentCursor = 0;
        int currentAvailableCursor = 0;

        List<Referer<T>> remoteReferers = new ArrayList<Referer<T>>();
        while (currentAvailableCursor < MAX_REFERER_COUNT && currentCursor < refererSize) {
            Referer<T> temp = referers.get((startIndex + currentCursor) % refererSize);
            currentCursor++;

            if (!temp.isAvailable() || localReferers.contains(temp)) {
                continue;
            }

            currentAvailableCursor++;

            remoteReferers.add(temp);
        }

        Collections.sort(remoteReferers, new LowActivePriorityComparator<T>());
        refersHolder.addAll(remoteReferers);
    
<methods>public non-sealed void <init>() ,public void init(com.weibo.api.motan.rpc.URL) ,public void onRefresh(List<Referer<T>>) ,public Referer<T> select(com.weibo.api.motan.rpc.Request) ,public void selectToHolder(com.weibo.api.motan.rpc.Request, List<Referer<T>>) ,public void setWeightString(java.lang.String) <variables>public static final int MAX_REFERER_COUNT,protected com.weibo.api.motan.rpc.URL clusterUrl,private List<Referer<T>> referers