EventCounter
The easiest way to count events.
##Importing to your project Add this dependency to your build.gradle file:
dependencies {
compile 'com.hrules:eventcounter:2.0.0'
}
##Usage This lib is easy to use. If you want to create a counter and increase or decrement his value, you only have to write these lines. There are two ways to do that: (The key value is only an ID for your counter, use any that you want)
- Create an EventCounter object and manage it:
public static final String EVENTCOUNTER_KEY = "KEY";
public static final int EVENTCOUNTER_DEFAULT_VALUE = 0;
public static final int EVENTCOUNTER_EXPECTED_VALUE = 5;
EventCounter eventCounter = new EventCounter(context);
// Increase value (Default increment 1, add an integer param to set value to increment)
eventCounter.increment(EVENTCOUNTER_KEY);
// Decrement value (Default decrement 1, add an integer param to set value to decrement)
eventCounter.decrement(EVENTCOUNTER_KEY);
// Get value
int value = eventCounter.get(EVENTCOUNTER_KEY);
int value = eventCounter.get(EVENTCOUNTER_KEY, EVENTCOUNTER_DEFAULT_VALUE); // default value to return
// Check value
boolean bool = eventCounter.check(EVENTCOUNTER_KEY, EVENTCOUNTER_EXPECTED_VALUE);
boolean bool = eventCounter.check(EVENTCOUNTER_KEY, EVENTCOUNTER_EXPECTED_VALUE, EventCounter.GREATER_THAN_OR_EQUAL_TO);
// Reset value
eventCounter.reset(EVENTCOUNTER_KEY);
// Reset all
eventCounter.resetAll();
// Add or remove key
eventCounter.add(EVENTCOUNTER_KEY);
eventCounter.remove(EVENTCOUNTER_KEY);
// Remove all
eventCounter.removeAll();
- Use it as singleton:
public static final String EVENTCOUNTER_KEY = "KEY";
public static final int EVENTCOUNTER_DEFAULT_VALUE = 0;
public static final int EVENTCOUNTER_EXPECTED_VALUE = 5;
// Increase value
EventCounter.with(context).increment(EVENTCOUNTER_KEY);
// Decrement value
EventCounter.with(context).decrement(EVENTCOUNTER_KEY);
// Get value
int value = EventCounter.with(context).get(EVENTCOUNTER_KEY);
int value = EventCounter.with(context).get(EVENTCOUNTER_KEY, EVENTCOUNTER_DEFAULT_VALUE); // default value to return
// Check value
boolean bool = EventCounter.with(context).check(EVENTCOUNTER_KEY, EVENTCOUNTER_EXPECTED_VALUE);
boolean bool = EventCounter.with(context).check(EVENTCOUNTER_KEY, EVENTCOUNTER_EXPECTED_VALUE, EventCounter.GREATER_THAN_OR_EQUAL_TO);
// Reset counter
EventCounter.with(context).reset(EVENTCOUNTER_KEY);
Readme added by Mun0n Nice to meet you! ;-)
##Code Example
public static final String EVENTCOUNTER_KEY = "KEY";
public static final int EVENTCOUNTER_EXPECTED_VALUE = 5;
if (EventCounter.with(this).increment(EVENTCOUNTER_KEY).check(EVENTCOUNTER_KEY, EVENTCOUNTER_EXPECTED_VALUE) {
// ...
// Reset counter
// EventCounter.with(this).reset(EVENTCOUNTER_KEY);
}
Developed by
License
Copyright 2015 Héctor de Isidro - hrules6872
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.