Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.replica.replicaisland;
     18 
     19 import java.util.Comparator;
     20 
     21 public class ChannelSystem extends BaseObject {
     22     private static final int CHANNEL_COUNT = 8;
     23     private static final ChannelComparator sChannelComparator = new ChannelComparator();
     24     private FixedSizeArray<Channel> mChannels;
     25     private Channel mSearchDummy;
     26     private int mRegisteredChannelCount;
     27 
     28     public ChannelSystem() {
     29         super();
     30         mChannels = new FixedSizeArray<Channel>(CHANNEL_COUNT);
     31         mChannels.setComparator(sChannelComparator);
     32         mSearchDummy = new Channel();
     33 
     34         for (int x = 0; x < CHANNEL_COUNT; x++) {
     35             mChannels.add(new Channel());
     36         }
     37 
     38         mRegisteredChannelCount = 0;
     39     }
     40 
     41     @Override
     42     public void reset() {
     43         for (int x = 0; x < CHANNEL_COUNT; x++) {
     44             mChannels.get(x).name = null;
     45             mChannels.get(x).value = null;
     46         }
     47 
     48         mRegisteredChannelCount = 0;
     49     }
     50 
     51     public Channel registerChannel(String name) {
     52         Channel result = null;
     53         mSearchDummy.name = name;
     54         final int index = mChannels.find(mSearchDummy, false);
     55         if (index == -1) {
     56             // Add a new channel.
     57             assert mRegisteredChannelCount < CHANNEL_COUNT : "Channel pool exhausted!";
     58 
     59             if (mRegisteredChannelCount < CHANNEL_COUNT) {
     60                 result = mChannels.get(mRegisteredChannelCount);
     61                 mRegisteredChannelCount++;
     62                 result.name = name;
     63                 mChannels.sort(true);
     64             }
     65         } else {
     66             result = mChannels.get(index);
     67         }
     68 
     69         return result;
     70     }
     71 
     72     public class Channel {
     73         public String name;
     74         public Object value;
     75     }
     76 
     77     public static class ChannelFloatValue {
     78         public float value;
     79     }
     80 
     81     public static class ChannelBooleanValue {
     82     	public boolean value;
     83     }
     84 
     85     /** Comparator for channels. */
     86     private final static class ChannelComparator implements Comparator<Channel> {
     87         public int compare(final Channel object1, final Channel object2) {
     88             int result = 0;
     89             if (object1 == null && object2 != null) {
     90                 result = 1;
     91             } else if (object1 != null && object2 == null) {
     92                 result = -1;
     93             } else if (object1 != null && object2 != null) {
     94                 if (object1.name == null && object2.name != null) {
     95                     result = 1;
     96                 } else if (object1.name != null && object2.name == null) {
     97                     result = -1;
     98                 } else if (object1.name != null && object2.name != null) {
     99                     result = object1.name.compareTo(object2.name);
    100                 }
    101             }
    102             return result;
    103         }
    104     }
    105 
    106 }
    107