001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.server.web; 020 021import java.util.concurrent.CountDownLatch; 022import java.util.concurrent.ExecutionException; 023 024import pt.ua.dicoogle.sdk.datastructs.SearchResult; 025import pt.ua.dicoogle.sdk.task.JointQueryTask; 026import pt.ua.dicoogle.sdk.task.Task; 027 028public class FirstResponserQueryHolder extends JointQueryTask { 029 030 private Iterable<SearchResult> result; 031 032 @Override 033 public void onReceive(Task<Iterable<SearchResult>> e) { 034 try { 035 synchronized (this) { 036 if(result == null){ 037 result = e.get(); 038 stopAllTaks(); 039 } 040 } 041 042 notifyAll(); 043 044 } catch (InterruptedException | ExecutionException e1) { 045 // TODO Auto-generated catch block 046 e1.printStackTrace(); 047 } 048 } 049 050 public FirstResponserQueryHolder(CountDownLatch latch) { 051 super(); 052 } 053 054 private void stopAllTaks() { 055 this.cancel(true); 056 } 057 058 @Override 059 public void onCompletion() { 060 // TODO: MAY BE BUGGED 061 notifyAll(); 062 } 063 064 public Iterable<SearchResult> getResult() { 065 synchronized (this) { 066 while( !(this.isCancelled() || this.isDone())){ 067 if(result != null) 068 return result; 069 070 try { 071 wait(); 072 } catch (InterruptedException e) { 073 // TODO Auto-generated catch block 074 e.printStackTrace(); 075 } 076 } 077 } 078 return null; 079 } 080 081 082 083 084 085 086}