Skip to content

Commit

Permalink
dayflags class+parser+tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janvde committed Aug 28, 2017
1 parent d003b98 commit 0f95361
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {

SearchRequest searchRequest = new SearchRequest.SearchRequestBuilder()
.oName("Delft")
.dName("Rotterdam")
.dName("London")
.noBus()
.noCar()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ public AirLeg[] newArray(int size) {
return new AirLeg[size];
}
};

@Override
public String toString() {
return "AirLeg{" +
"operatingDays=" + operatingDays +
", indicativePrices=" + indicativePrices +
", hops=" + hops +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,46 @@

/**
* Created by jan on 11/07/16.
*
* todo check how to parse this from json
*/

public class DayFlags {
private byte flag;
public static final int SUNDAY = 1;
public static final int MONDAY = 2;
public static final int TUESDAY = 4;
public static final int WEDNESDAY = 8;
public static final int THURSDAY = 16;
public static final int FRIDAY = 32;
public static final int SATURDAY = 64;

public static final int EVERYDAY = SUNDAY + MONDAY + TUESDAY + WEDNESDAY + THURSDAY + FRIDAY + SATURDAY;
public static final int WEEKENDS = SATURDAY + SUNDAY;
public static final int WEEKDAYS = MONDAY + TUESDAY + WEDNESDAY + THURSDAY + FRIDAY;
public static final int NEVER = 0;


private int flag;

public DayFlags(int flag) {
this.flag = flag;
}

public int getFlag() {
return flag;
}

public void setFlag(int flag) {
this.flag = flag;
}


/**
* is given day flagged
* use static integer from this class
* @param day static int above
* @return boolean
*/
public boolean isDay(int day) {
return (flag & day) == day;
}

// TODO: 11/07/16 xor flags
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nl.endevelopment.r2randroid.r2rlib.parser;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

import nl.endevelopment.r2randroid.r2rlib.models.DayFlags;

/**
* Created by jan on 28/08/2017.
*/

public class DayFlagsParser implements JsonDeserializer<DayFlags> {
@Override
public DayFlags deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

//Expected input should be a int in the [0-127] range (7 bytes)
int intFlag = json.getAsInt();

if (intFlag < 0 || intFlag > 127) {
return new DayFlags(0);
}

return new DayFlags(intFlag);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;

import nl.endevelopment.r2randroid.r2rlib.models.DayFlags;
import nl.endevelopment.r2randroid.r2rlib.parser.BooleanParser;
import nl.endevelopment.r2randroid.r2rlib.parser.DayFlagsParser;
import nl.endevelopment.r2randroid.r2rlib.utils.ConnectionUtils;
import okhttp3.Cache;
import okhttp3.HttpUrl;
Expand All @@ -29,7 +34,7 @@ public Rome2RioApiClient(Context context, String key) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(getGsonConverterFactory())
.client(getClient(key))
.build();

Expand Down Expand Up @@ -81,4 +86,21 @@ public Response intercept(Chain chain) throws IOException {
}


/**
* get gson converter factory
* register type adapters here
* @return GsonConverterFactory
*/
public GsonConverterFactory getGsonConverterFactory(){
Gson gson = new GsonBuilder()
.registerTypeAdapter(Boolean.class, new BooleanParser())
.registerTypeAdapter(DayFlags.class, new DayFlagsParser())
.create();

GsonConverterFactory factory = GsonConverterFactory.create(gson);

return factory;
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nl.endevelopment.r2randroid.r2rlib.models;

import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
* Created by jan on 28/08/2017.
* test dayflag bitwise and operator
*/

public class DayFlagsTest {
@Test
public void isSunday() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.SUNDAY);

assertThat(dayFlags.isDay(DayFlags.SUNDAY), is(true));
}

@Test
public void isWeekday() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.WEEKDAYS);

assertThat(dayFlags.isDay(DayFlags.FRIDAY), is(true));
}

@Test
public void isNotWeekday() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.WEEKDAYS);

assertThat(dayFlags.isDay(DayFlags.SUNDAY), is(false));
}

@Test
public void isWeekend() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.WEEKENDS);

assertThat(dayFlags.isDay(DayFlags.SATURDAY), is(true));
}

@Test
public void isNotWeekend() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.WEEKENDS);

assertThat(dayFlags.isDay(DayFlags.THURSDAY), is(false));
}

@Test
public void isAlways() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.EVERYDAY);

assertThat(dayFlags.isDay(DayFlags.SATURDAY), is(true));
}

@Test
public void isAlwaysWeekend() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.EVERYDAY);

assertThat(dayFlags.isDay(DayFlags.WEEKENDS), is(true));
}

@Test
public void isNever() throws Exception {
DayFlags dayFlags = new DayFlags(DayFlags.NEVER);

assertThat(dayFlags.isDay(DayFlags.SATURDAY), is(false));
}

}

0 comments on commit 0f95361

Please sign in to comment.