Back to project page SpunkyCharts.
The source code is released under:
GNU General Public License
If you think the Android project SpunkyCharts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jogden.spunkycharts.data; /*w ww.j a va2 s . com*/ import java.util.Deque; import java.util.Random; import java.util.concurrent.LinkedBlockingDeque; import android.text.format.Time; import android.util.Log; import com.jogden.spunkycharts.data.DataContentService.DataClientInterface; import com.jogden.spunkycharts.misc.OHLC; import com.jogden.spunkycharts.misc.Pair; import com.jogden.spunkycharts.data.DataContentService; /* Copyright (C) 2014 Jonathon Ogden < jeog.dev@gmail.com > This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses. */ public class DataClientLocalDebug implements DataContentService.DataClientInterface { static public final int DATA_PRICE_DEF = 1; static public final int DATA_VOL_DEF = 2; static private final int REF_RATE = 1000; // put a limit on til we write code for pulling subs private Deque<Integer> retVolVals = new LinkedBlockingDeque<Integer>(1000); private Deque<OHLC> retPriceVals = new LinkedBlockingDeque<OHLC>(1000); private Random rand = new Random(); private Time tempTime = new Time(); private Time lastSegTime = new Time(); private Thread _thread = null; private DataClientInterface _this = this; private DataClientLocalDebug _myMonitor = this; @Override public boolean connect() { return start(); } @Override public boolean disconnect() { stop(false); return true; } public final boolean running() { synchronized(this){ return (_thread != null); } } @SuppressWarnings("unchecked") @Override //sync?? public synchronized <T> Pair<T[],Time> getBulk(String symbol, Time start, int type) { try{ if(type == DATA_PRICE_DEF){ OHLC[] tmpA = new OHLC[retPriceVals.size()]; retPriceVals.toArray(tmpA); return new Pair<T[],Time>( (T[])tmpA, lastSegTime ); } else if(type == DATA_VOL_DEF){ Integer[] tmpA = new Integer[retVolVals.size()]; retVolVals.toArray(tmpA); return new Pair<T[],Time>( (T[])tmpA, lastSegTime ); } else throw new DataClientException( "invalid type paramater passed to getBulk" ); }catch(DataClientException e){ throw e; }catch(ClassCastException e){ throw new DataClientException( "invalid return type expected from getBulk" ); }catch(RuntimeException e){ throw new DataClientException(e); } } @SuppressWarnings("unchecked") @Override public synchronized <T> Pair<T,Time> getLast(String symbol, int type) { try{ if(type == DATA_PRICE_DEF) return new Pair<T,Time>( (T)(retPriceVals.peekLast()), lastSegTime ); else if(type == DATA_VOL_DEF) return new Pair<T,Time>( (T)(retVolVals.peekLast()), lastSegTime ); else throw new DataClientException( "invalid type paramater passed to getBulk" ); }catch(DataClientException e){ throw e; }catch(ClassCastException e){ throw new DataClientException( "invalid return type expected from getBulk" ); }catch(ArrayIndexOutOfBoundsException e){ throw new DataClientException( "data doesn't exist at this index" ); }catch(RuntimeException e){ throw new DataClientException(e); } } private class doInBackground extends Thread { private final int START = 0; private final int END = 23; @Override public void run(){ float o, h, l, c; int i; Random rand = new Random(); for (i = START, o = 30f, h = 35f, l = 28f, c = 32f; i < END; ) { OHLC price = new OHLC(o,h,l,c); int vol = (int)Math.abs(rand.nextFloat() * 1000); retPriceVals.offer(price); retVolVals.offer(vol); /* Log.d("Data-Client-BulkCreate", String.valueOf(price.open) + " " + String.valueOf(price.high) + " " + String.valueOf(price.low) + " " + String.valueOf(price.close) +" " + String.valueOf(vol) ); */ o = c; float rIncr = rand.nextFloat(); if (rand.nextInt() < 0){ c+=rIncr; h+=rIncr; l+=rIncr; } else { c-=rIncr; h-=rIncr; l-=rIncr; } i++; } lastSegTime.setToNow(); float ii = 0; float[] f3Tup; try{ do { synchronized(_myMonitor){ f3Tup = _generateRandomData(ii); tempTime.setToNow(); if( lastSegTime.hour == tempTime.hour && lastSegTime.minute == tempTime.minute){ OHLC price = retPriceVals.peekLast(); price.close = f3Tup[0]; if(f3Tup[0] < price.low) price.low = f3Tup[0]; if(f3Tup[0] > price.high) price.high = f3Tup[0]; retVolVals.offerLast( retVolVals.pollLast() + (int)f3Tup[1] ); /* Log.d("Data-Client-Update-Current", price.toString() +" " + String.valueOf(retVolVals.peekLast()) );*/ }else{ OHLC price = new OHLC(f3Tup[0]); if( !retPriceVals.offer(price) ){ retPriceVals.pollFirst(); retVolVals.pollFirst(); retPriceVals.offer(price); retVolVals.offer((int)f3Tup[1]); }else retVolVals.offer((int)f3Tup[1]); /* Log.d("Data-Client-Update-New", price.toString() +" " + String.valueOf(retVolVals.peekLast()) ); */ } lastSegTime.set(tempTime); } Thread.sleep( REF_RATE ); if (f3Tup[2] < 0) ii-=f3Tup[3]; else ii+=f3Tup[3]; } while( true ); }catch(InterruptedException e){ }catch(RuntimeException e){ throw e; }finally{ synchronized(_this){ _thread = null; _this.notify(); } } } } private final float[] _generateRandomData(float iter) { float price = 30f + iter; float vol = Math.abs(2 * rand.nextFloat() * iter *2); return new float[]{ price,vol, (float)rand.nextInt(), rand.nextFloat() }; } private final void stop(boolean blockUntilStopped) { synchronized(this){ if(_thread != null) _thread.interrupt(); if(blockUntilStopped) while( _thread != null){ try { this.wait(); } catch (InterruptedException e) {} } } } private final boolean start() { synchronized(this){ if(_thread == null) { _thread = new doInBackground(); try{ _thread.start(); }catch(RuntimeException e){ e.printStackTrace(); } return true; } } return false; } }