/*
 * This file is part of libbluray
 * Copyright (C) 2010  William Hahne
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package javax.tv.service.navigation;

import javax.tv.service.SIRetrievable;
import javax.tv.service.Service;
import javax.tv.service.SIElement;
import javax.tv.service.SIRequest;
import javax.tv.service.SIRequestorImpl;

public final class SIElementFilter extends ServiceFilter
{
    public SIElementFilter(SIElement element) throws FilterNotSupportedException {
        this.element = element;
    }

    public SIElement getFilterValue() {
        return element;
    }

    public boolean accept(Service service) {
        SIRequestorImpl requestor = new SIRequestorImpl();

        SIRequest req = service.retrieveDetails(requestor);

        // TODO: This may be a bit excessive
        int timeout = 0;
        while (!requestor.getResponse() && timeout < 1000) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // ignore
            }

            timeout++;
        }

        // if we still don't have a response just cancel the request
        if (!requestor.getResponse()) {
            if (req != null)
                req.cancel();
        }

        if (requestor.getResult() == null)
            return false;

        SIRetrievable[] rets = requestor.getResult();
        for (int i = 0; i < rets.length; i++) {
            if (rets[i].equals(element))
                return true;
        }

        return false;
    }

    SIElement element;
}

@@ -67,16 +67,57 @@
 m Hahne%0A
+ * Copyright (C) 2015  Petri Hintukainen%0A
  *%0A * Th
@@ -1009,16 +1009,50 @@
 rImpl;%0A%0A
+import org.bluray.net.BDLocator;%0A%0A
 public f
@@ -1185,16 +1185,372 @@
 ption %7B%0A
+        if (element == null)%0A            throw new NullPointerException();%0A%0A        try %7B%0A            new BDLocator(element.getLocator().toExternalForm());%0A        %7D catch (Exception e) %7B%0A            System.err.println(%22Invalid SI element: %22 + e + %22 at %22 + org.videolan.Logger.dumpStack(e));%0A            throw new FilterNotSupportedException();%0A        %7D%0A%0A
         

validate locator