package io.joynr.dispatching.subscription;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2013 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import io.joynr.provider.Deferred;
import io.joynr.provider.Promise;

import java.util.ArrayList;
import java.util.List;

import joynr.exceptions.ProviderRuntimeException;
import joynr.tests.DefaulttestProvider;
import joynr.types.Localisation.GpsFixEnum;
import joynr.types.Localisation.GpsLocation;

public class SubscriptionTestsProviderImpl extends DefaulttestProvider {

    public static final String MESSAGE_PROVIDERRUNTIMEEXCEPTION = "ProviderRuntimeException";
    public static final String MESSAGE_THROWN_PROVIDERRUNTIMEEXCEPTION = "thrownException";

    List<Integer> list = new ArrayList<Integer>();

    public SubscriptionTestsProviderImpl() {
        testAttribute = 42;
        complexTestAttribute = new GpsLocation();
        complexTestAttribute.setLatitude(48.143554);
        complexTestAttribute.setLongitude(11.536564);
        complexTestAttribute.setAltitude(6.0);
        complexTestAttribute.setGpsFix(GpsFixEnum.MODE3D);
        ATTRIBUTEWITHCAPITALLETTERS = 42;
    }

    @Override
    public Promise<Deferred<Integer>> getTestAttribute() {
        Deferred<Integer> deferred = new Deferred<Integer>();
        deferred.resolve(testAttribute++);
        return new Promise<Deferred<Integer>>(deferred);
    }

    @Override
    public Promise<Deferred<Integer[]>> getListOfInts() {
        Deferred<Integer[]> deferred = new Deferred<Integer[]>();
        list.add(testAttribute++);
        deferred.resolve(list.toArray(new Integer[list.size()]));
        return new Promise<Deferred<Integer[]>>(deferred);
    }

    public GpsLocation getComplexTestAttributeSync() {
        return complexTestAttribute;
    }

    @Override
    public Promise<Deferred<Integer>> getAttributeWithProviderRuntimeException() {
        Deferred<Integer> deferred = new Deferred<Integer>();
        ProviderRuntimeException error = new ProviderRuntimeException(MESSAGE_PROVIDERRUNTIMEEXCEPTION);
        deferred.reject(error);
        return new Promise<Deferred<Integer>>(deferred);
    }

    @Override
    public Promise<Deferred<Integer>> getAttributeWithThrownException() {
        throw new IllegalArgumentException(MESSAGE_THROWN_PROVIDERRUNTIMEEXCEPTION);
    }

}

@@ -747,57 +747,159 @@
 se;%0A
-%0Aimport java.util.ArrayList;%0Aimport java.util.Lis
+import io.joynr.pubsub.publication.AttributeListener;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A
@@ -2962,10 +2962,814 @@
 %0A    %7D%0A%0A
+    Set%3CString%3E attributeSubscriptionArrived = new HashSet%3C%3E();%0A%0A    public void waitForAttributeSubscription(String attributeName) %7B%0A        synchronized (this) %7B%0A            while (!attributeSubscriptionArrived.contains(attributeName)) %7B%0A                try %7B%0A                    this.wait();%0A                %7D catch (InterruptedException e) %7B%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void registerAttributeListener(String attributeName,%0A    %09%09AttributeListener attributeListener) %7B%0A        super.registerAttributeListener(attributeName, attributeListener);%0A        synchronized (this) %7B%0A        %09if (!attributeSubscriptionArrived.contains(attributeName)) %7B%0A        %09%09attributeSubscriptionArrived.add(attributeName);%0A        %09%09this.notify();%0A        %09%7D%0A        %7D%0A    %7D%0A
 %7D%0A

test provider allows to sync for attribute registrations