package io.realm.internal.android;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * This thread continuously receives and dispatches messages regarding changes in realms.
 */
public class LooperThread extends Thread {
    // Message types
    public static final int REALM_CHANGED  = 3;

    public static final Map<Handler, Integer> handlers = new ConcurrentHashMap<Handler, Integer>();
    public Handler handler;
    private static LooperThread instance;

    // private because it's a singleton
    private LooperThread() {}

    // thread safe static constructor
    public static LooperThread getInstance() {
        if (instance == null) {
            synchronized (LooperThread.class) {
                if (instance == null) {
                    instance = new LooperThread();
                }
            }
        }
        return instance;
    }

    @Override
    public void run() {
        Looper.prepare();

        handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                if (message.arg1 == REALM_CHANGED) {
                    for (Map.Entry<Handler, Integer> entry : handlers.entrySet()) {
                        if (entry.getValue() == message.arg2) {
                            if (!entry.getKey().hasMessages(REALM_CHANGED)) {
                                entry.getKey().sendEmptyMessage(REALM_CHANGED);
                            }
                        }
                    }
                }
            }
        };

        Looper.loop();
    }
}

@@ -1,8 +1,599 @@
+/*%0A * Copyright 2014 Realm Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

Add license to the new file