Java tutorial
/******************************************************************************* * Copyright (c) 2014,2015 Hideki Yatomi * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html ******************************************************************************/ package net.yatomiya.nicherry.services.bbs; import java.io.*; import java.util.*; import com.squareup.okhttp.*; import net.yatomiya.e4.util.*; import net.yatomiya.nicherry.*; import net.yatomiya.nicherry.model.bbs.*; import net.yatomiya.nicherry.services.bbs.model.*; class BoardUpdateHandler extends ModelUpdateHandler { byte[] subjectTxtBytes; Analyzer analyzer; @Override public BoardProcessor getProcessor() { return (BoardProcessor) super.getProcessor(); } @Override public MBoard getModel() { return (MBoard) super.getModel(); } @Override protected UpdateEvent createUpdateEvent() { return new BoardUpdateEvent(); } @Override protected String createRequestUrl() { return getModel().getUrl() + "subject.txt"; } @Override protected boolean handleBackground(Response response) throws IOException { subjectTxtBytes = response.body().bytes(); MBoard model = getModel(); getHttpClient().download(model.getUrl() + "setting.txt", new Date(model.getSettingTxtLastModifiedInResponseHeader()), isSynchronous(), (r) -> processSettingTxt(r)); getHttpClient().download(model.getUrl() + "head.txt", new Date(model.getHeadTxtLastModifiedInResponseHeader()), isSynchronous(), (r) -> processHeadTxt(r)); String text = StringUtils.getString(subjectTxtBytes, NConstants.CHARSET_SHIFT_JIS); List<MThreadItem> updateItems = new SubjectTxtParser().parse(getModel(), text); analyzer = new Analyzer(); analyzer.analyze(updateItems); return true; } @Override protected boolean handleForeground() throws IOException { try { IOUtils.write(new File(getStorageDirectory(), "subject.txt"), subjectTxtBytes); } catch (IOException e) { // don't care, just skip. } analyzer.apply(); getProcessor().resetAnalyzedStates(); return true; } class Analyzer { // Board.threadItems List<MThreadItem> currentItems; // Board.kakoThreadItems List<MThreadItem> kakoItems; // ?????????????item List<MThreadItem> toKakoItems; // ????????????????????????item List<MThreadItem> recoveredKakoItems; // ??? ThreadItem List<MThreadItem> updateItems; // ????? ThreadItem List<MThreadItem> updateKakoItems; void analyze(List<MThreadItem> updateItems) { this.updateItems = updateItems; MBoard board = getModel(); currentItems = board.getThreadItems(); kakoItems = board.getKakoThreadItems(); toKakoItems = CUtils.filter(currentItems, item -> CUtils.noneMatch(updateItems, uItem -> uItem.getThreadId().equals(item.getThreadId()))); recoveredKakoItems = CUtils.filter(kakoItems, item -> CUtils.anyMatch(updateItems, uItem -> uItem.getThreadId().equals(item.getThreadId()))); updateKakoItems = new ArrayList<>(kakoItems); updateKakoItems.addAll(toKakoItems); updateKakoItems.removeAll(recoveredKakoItems); // ??????? for (MThreadItem item : updateItems) { MThreadItem oldItem = CUtils.findFirst(currentItems, cItem -> cItem.getThreadId().equals(item.getThreadId())); if (oldItem != null) { item.setMessageCountInDat(oldItem.getMessageCountInDat()); item.setLastMessageCountInDat(oldItem.getLastMessageCountInDat()); } } } void apply() { MBoard board = getModel(); for (MThreadItem item : new ArrayList<>(toKakoItems)) { // ??dat???? int datNo; try { datNo = Integer.valueOf(item.getThreadId().getIdValue()); } catch (NumberFormatException e) { datNo = 0; } item.setNo(datNo); // Thread ????????????? if (getProcessor().getManager().getBBSService().getThreadManager() .isStorageExists(item.getThreadId())) { item.setKako(true); } else { updateKakoItems.remove(item); } } board.getThreadItems().clear(); board.getThreadItems().addAll(updateItems); board.getKakoThreadItems().clear(); board.getKakoThreadItems().addAll(updateKakoItems); } } void processSettingTxt(Response response) { if (response == null) return; if (response.code() != 200) return; try { byte[] content = response.body().bytes(); if (content.length > 0) { File file = getProcessor().getSettingTxtFile(); IOUtils.write(file, content); getProcessor().loadSettingTxt(); } } catch (IOException e) { } } void processHeadTxt(Response response) { if (response == null) return; if (response.code() != 200) return; try { byte[] content = response.body().bytes(); if (content.length > 0) { File file = getProcessor().getHeadTxtFile(); IOUtils.write(file, content); } } catch (IOException e) { } } @Override protected String getFinishedAnnounceMessage() { return ""; } }