Android Open Source - latrobe-datacapture-dir Data






From Project

Back to project page latrobe-datacapture-dir.

License

The source code is released under:

MIT License

If you think the Android project latrobe-datacapture-dir listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.DataCaptureApp.data;
/* ww  w .j  a v a2s . c  o m*/
import com.example.DataCaptureApp.utils.JSONReader;

import java.io.Serializable;
import java.util.*;

/**
 * Created by Tom on 6/09/2014.
 */
public class Data implements Serializable
{
    private Map<String, Object> mMap = new HashMap<String, Object>();

    public Data() {}

    public <T> Data(String key, T value)
    {
        set(key, value);
    }

    public <T> T get(String key)
    {
        return (T)mMap.get(key);
    }

    public <T> void set(String key, T value)
    {
        mMap.put(key, value);
    }

    public <T> T remove(String key)
    {
        return (T)mMap.remove(key);
    }

    public boolean contains(String key)
    {
        return mMap.containsKey(key);
    }

    public boolean contains(String key, Class c)
    {
        Object obj = mMap.get(key);
        return c.isInstance(obj);
    }

    public Set<Field> getFields()
    {
        Set<Map.Entry<String, Object>> entries = mMap.entrySet();
        Set<Field> fields = new HashSet<Field>();
        for(Map.Entry<String, Object> entry : entries)
        {
            fields.add(new Field(entry.getKey(), entry.getValue()));
        }
        return fields;
    }

    public void clear()
    {
        mMap.clear();
    }

    // Note: Primitive arrays not transformed correctly!
    public String toJson()
    {
        StringBuilder json = new StringBuilder();
        json.append('{');
        Set<Map.Entry<String, Object>> entries = mMap.entrySet();
        boolean isFirst = true;
        for(Map.Entry<String, Object> entry : entries)
        {
            if(entry.getValue() != null)
            {
                // Comma separator (applied on second and subsequent iterations
                if (!isFirst)
                    json.append(",");
                isFirst = false;
                // Add key
                json.append("\"" + entry.getKey() + "\":");
                // Add value
                json.append(toJson(entry.getValue()));
            }
        }
        json.append('}');
        return json.toString();
    }

    private String toJson(Object obj)
    {
        String json = "";
        // Determine value string
        if(obj instanceof Number || obj instanceof Boolean)
        {
            json += obj.toString();
        }
        else if(obj instanceof Object[])
        {
            Object[] array = (Object[])obj;
            json += '[';
            boolean isFirst = true;
            for(Object element : array)
            {
                if(element != null)
                {
                    if (!isFirst)
                        json += ',';
                    isFirst = false;
                    json += toJson(element);
                }
            }
            json += ']';
        }
        else if(obj instanceof Data)
        {
            json += ((Data) obj).toJson();
        }
        else
        {
            json += "\"" + obj.toString() + "\"";
        }
        return json;
    }

    // Note: Arrays always stored as object arrays!
    public static Data fromJson(String json)
    {
        JSONReader reader = new JSONReader();
        Object obj = reader.read(json);
        if(!(obj instanceof Map))
            return null;
        Data data = (Data)getObject(obj);
        return data;
    }

    private static Object getObject(Object jsonObj)
    {
        if(jsonObj instanceof Map)
        {
            Data data = new Data();
            Map map = (Map) jsonObj;
            Set<Map.Entry> entries = map.entrySet();
            for (Map.Entry entry : entries)
            {
                String key = entry.getKey().toString();
                Object value = getObject(entry.getValue());
                data.set(key, value);
            }
            return data;
        }
        else if(jsonObj instanceof Collection)
        {
            Object[] objArr = ((Collection) jsonObj).toArray();
            for(int i = 0; i < objArr.length; ++i)
            {
                objArr[i] = getObject(objArr[i]);
            }
            return objArr;
        }
        return jsonObj;
    }

    public class Field
    {
        private String mKey;
        private Object mValue;

        public Field(String key, Object value)
        {
            mKey = key;
            mValue = value;
        }

        public String getKey()
        {
            return mKey;
        }

        public Object getValue()
        {
            return mValue;
        }
    }
}




Java Source Code List

com.example.DataCaptureApp.AdvConfigActivity.java
com.example.DataCaptureApp.ConfigActivity.java
com.example.DataCaptureApp.MasterActivity.java
com.example.DataCaptureApp.MasterService.java
com.example.DataCaptureApp.SlaveActivity.java
com.example.DataCaptureApp.SlaveService.java
com.example.DataCaptureApp.data.DataServiceConnection.java
com.example.DataCaptureApp.data.DataService.java
com.example.DataCaptureApp.data.DataTransform.java
com.example.DataCaptureApp.data.Data.java
com.example.DataCaptureApp.data.Event.java
com.example.DataCaptureApp.data.FailedInitialisationException.java
com.example.DataCaptureApp.data.IDataEventListener.java
com.example.DataCaptureApp.data.IDataListener.java
com.example.DataCaptureApp.data.IDataSource.java
com.example.DataCaptureApp.data.IDataTransform.java
com.example.DataCaptureApp.data.IEventListener.java
com.example.DataCaptureApp.data.IEventSource.java
com.example.DataCaptureApp.services.BluetoothConnectivityService.java
com.example.DataCaptureApp.services.BluetoothThread.java
com.example.DataCaptureApp.services.DataDbContract.java
com.example.DataCaptureApp.services.DataDbHelper.java
com.example.DataCaptureApp.services.DataStoreService.java
com.example.DataCaptureApp.services.HttpThread.java
com.example.DataCaptureApp.services.RemoteConnectivityService.java
com.example.DataCaptureApp.services.SensorSampleService.java
com.example.DataCaptureApp.services.SensorSampler.java
com.example.DataCaptureApp.testing.BluetoothActivity.java
com.example.DataCaptureApp.testing.DataStoreActivity.java
com.example.DataCaptureApp.testing.DataTester.java
com.example.DataCaptureApp.testing.IServiceListener.java
com.example.DataCaptureApp.testing.MainService.java
com.example.DataCaptureApp.testing.MasterTestActivity.java
com.example.DataCaptureApp.testing.RandomService.java
com.example.DataCaptureApp.testing.RemoteActivity.java
com.example.DataCaptureApp.testing.SensorSampleActivity.java
com.example.DataCaptureApp.testing.ServiceTestActivity.java
com.example.DataCaptureApp.testing.SlaveTestActivity.java
com.example.DataCaptureApp.testing.TestActivity.java
com.example.DataCaptureApp.transforms.AggregatorDataTransform.java
com.example.DataCaptureApp.transforms.ArithmeticDataTransform.java
com.example.DataCaptureApp.transforms.ArrayCollectDataTransform.java
com.example.DataCaptureApp.transforms.ArraySplitDataTransform.java
com.example.DataCaptureApp.transforms.DeserialiseDataTransform.java
com.example.DataCaptureApp.transforms.FieldCopyDataTransform.java
com.example.DataCaptureApp.transforms.FieldModifyDataTransform.java
com.example.DataCaptureApp.transforms.FieldRenameDataTransform.java
com.example.DataCaptureApp.transforms.IntervalAggregatorDataTransform.java
com.example.DataCaptureApp.transforms.PackDataTransform.java
com.example.DataCaptureApp.transforms.QuaternionDifferenceDataTransform.java
com.example.DataCaptureApp.transforms.RemoveDataTransform.java
com.example.DataCaptureApp.transforms.SetDataTransform.java
com.example.DataCaptureApp.transforms.UnpackDataTransform.java
com.example.DataCaptureApp.utils.BroadcastDataSource.java
com.example.DataCaptureApp.utils.ByteUtils.java
com.example.DataCaptureApp.utils.DataEventHandler.java
com.example.DataCaptureApp.utils.DataHandlerThread.java
com.example.DataCaptureApp.utils.JSONReader.java
com.example.DataCaptureApp.utils.Quaternion.java
com.example.DataCaptureApp.utils.SerialisationUtils.java