Skip to content

Instantly share code, notes, and snippets.

@aruke
Created August 27, 2018 13:06
Show Gist options
  • Save aruke/0b88bf3e3780b989a77c621365dbd635 to your computer and use it in GitHub Desktop.
Save aruke/0b88bf3e3780b989a77c621365dbd635 to your computer and use it in GitHub Desktop.
A file collection showing use of dummy data class.
package com.example.data;
import java.util.ArrayList;
public class DummyData {
/**
* An {@link ArrayList} to store data.
*/
private static ArrayList<Model> mData;
/**
* Private constructor to prevent instantiation
*/
private DummyData() {
}
/*
* This code block runs when the class is loaded. Initialize any data here.
*/
static {
// Initialize objects
mData = new ArrayList<>();
// Add data manually to get diverse data
mData.add(new Model("String A", 13, true));
mData.add(new Model("String B", 53, false));
mData.add(new Model("String C", 119, true));
// Or just use a loop to generate monotonous data
for (int i = 0; i < 10; i++) {
String string = "String " + i;
mData.add(new Model(string, i, (i % 2 == 0)));
}
}
/**
* Returns {@link Model} data.
*
* @return ArrayList of Model objects.
*/
public static ArrayList<Model> getData() {
return mData;
}
}
package com.example.data;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// You can use the DummyData class anywhere in the app now. The data provided by DummyData will be consitent.
ArrayList<Model> modelArrayList = DummyData.getData();
}
}
package com.example.data;
public class Model {
private String mStringData;
private int mIntData;
private boolean mBoolData;
public Model(String stringData, int intData, boolean boolData) {
mStringData = stringData;
mIntData = intData;
mBoolData = boolData;
}
public String getStringData() {
return mStringData;
}
public void setStringData(String stringData) {
mStringData = stringData;
}
public int getIntData() {
return mIntData;
}
public void setIntData(int intData) {
mIntData = intData;
}
public boolean isBoolData() {
return mBoolData;
}
public void setBoolData(boolean boolData) {
mBoolData = boolData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment