com.google.firebase.database.GenericTypeIndicator Java Examples

The following examples show how to use com.google.firebase.database.GenericTypeIndicator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomClassMapper.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a standard library Java representation of JSON data to an object of the class provided
 * through the GenericTypeIndicator
 *
 * @param object The representation of the JSON data
 * @param typeIndicator The indicator providing class of the object to convert to
 * @return The POJO object.
 */
public static <T> T convertToCustomClass(Object object, GenericTypeIndicator<T> typeIndicator) {
  Class<?> clazz = typeIndicator.getClass();
  Type genericTypeIndicatorType = clazz.getGenericSuperclass();
  if (genericTypeIndicatorType instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) genericTypeIndicatorType;
    if (!parameterizedType.getRawType().equals(GenericTypeIndicator.class)) {
      throw new DatabaseException(
          "Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
    }
    // We are guaranteed to have exactly one type parameter
    Type type = parameterizedType.getActualTypeArguments()[0];
    return deserializeToType(object, type);
  } else {
    throw new DatabaseException(
        "Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
  }
}
 
Example #2
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleWriteValuesReconnectRead()
    throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  final DatabaseReference writer = refs.get(0);
  final DatabaseReference reader = refs.get(1);

  writer.child("a").child("b").child("c").setValueAsync(1);
  writer.child("a").child("d").child("e").setValueAsync(2);
  writer.child("a").child("d").child("f").setValueAsync(3);
  WriteFuture writeFuture = new WriteFuture(writer.child("g"), 4);
  writeFuture.timedGet();

  Map<String, Object> expected = new MapBuilder()
      .put("a",
          new MapBuilder().put("b", new MapBuilder().put("c", 1L).build())
              .put("d", new MapBuilder().put("e", 2L).put("f", 3L).build()).build())
      .put("g", 4L).build();
  ReadFuture readFuture = new ReadFuture(reader);

  GenericTypeIndicator<Map<String, Object>> t = new GenericTypeIndicator<Map<String, Object>>() {
  };
  Map<String, Object> result = readFuture.timedGet().get(0).getSnapshot().getValue(t);
  TestHelpers.assertDeepEquals(expected, result);
}
 
Example #3
Source File: CustomClassMapper.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a standard library Java representation of JSON data to an object of the class provided
 * through the GenericTypeIndicator
 *
 * @param object The representation of the JSON data
 * @param typeIndicator The indicator providing class of the object to convert to
 * @return The POJO object.
 */
public static <T> T convertToCustomClass(Object object, GenericTypeIndicator<T> typeIndicator) {
  Class<?> clazz = typeIndicator.getClass();
  Type genericTypeIndicatorType = clazz.getGenericSuperclass();
  if (genericTypeIndicatorType instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) genericTypeIndicatorType;
    if (!parameterizedType.getRawType().equals(GenericTypeIndicator.class)) {
      throw new DatabaseException(
          "Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
    }
    // We are guaranteed to have exactly one type parameter
    Type type = parameterizedType.getActualTypeArguments()[0];
    return deserializeToType(object, type);
  } else {
    throw new DatabaseException(
        "Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
  }
}
 
Example #4
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 6 votes vote down vote up
public static AgendaSection toAgendaSection(final DataSnapshot data) {
    final AgendaSection section = new AgendaSection();
    populateSection(data, section);
    Map<String, AgendaItem> items = data.child("items").getValue(new GenericTypeIndicator<Map<String, AgendaItem>>() {});
    if (items == null) {
        items = Collections.emptyMap();
    }

    // Force empty speaker list when null so the client doesn't have to
    // always check null.
    for (final Map.Entry<String, AgendaItem> entry : items.entrySet()) {
        if (entry.getValue().getSpeakerIds() == null) {
            entry.getValue().setSpeakerIds(Collections.<String>emptyList());
        }
    }

    section.setItems(items);
    return section;
}
 
Example #5
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
public static List<FeedbackQuestion> toFeedbackQuestions(final DataSnapshot data) {
    final List<FeedbackQuestion2> questions = data.getValue(new GenericTypeIndicator<List<FeedbackQuestion2>>() {});
    int size = questions != null ? questions.size() : 0;
    ArrayList<FeedbackQuestion> qs = new ArrayList<>(size);
    if (questions != null) {
        for (final FeedbackQuestion2 q : questions) {
            qs.add(new FeedbackQuestion(QuestionType.valueOf(q.getType()), q.getText()));
        }
    }
    return qs;

}
 
Example #6
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
public static MapsSection toMapsSection(DataSnapshot data) {
    final MapsSection section = new MapsSection();
    populateSection(data, section);
    Map<String, MapItem> items = data.child("items").getValue(new GenericTypeIndicator<Map<String, MapItem>>() {});
    if (items == null) {
        items = Collections.emptyMap();
    }
    section.setItems(items);
    return section;
}
 
Example #7
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
public static CompaniesSection toCompaniesSection(final DataSnapshot data) {
    final CompaniesSection section = new CompaniesSection();
    populateSection(data, section);
    Map<String, CompanyItem> items = data.child("items").getValue(new GenericTypeIndicator<Map<String, CompanyItem>>() {});
    if (items == null) {
        items = Collections.emptyMap();
    }
    section.setItems(items);
    return section;
}
 
Example #8
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
public static AttendeesSection toAttendeesSection(final DataSnapshot data) {
    final AttendeesSection section = new AttendeesSection();
    populateSection(data, section);
    Map<String, AttendeeItem> items = data.child("items").getValue(new GenericTypeIndicator<Map<String, AttendeeItem>>() {});
    if (items == null) {
        items = Collections.emptyMap();
    }
    section.setItems(items);
    return section;
}
 
Example #9
Source File: FirebaseDatabaseHelpers.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
public static SpeakersSection toSpeakersSection(final DataSnapshot data) {
    final SpeakersSection section = new SpeakersSection();
    populateSection(data, section);
    Map<String, SpeakerItem> items = data.child("items").getValue(new GenericTypeIndicator<Map<String, SpeakerItem>>() {});
    if (items == null) {
        items = Collections.emptyMap();
    }
    section.setItems(items);
    return section;
}
 
Example #10
Source File: SessionFeedbackFragment.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    LOGGER.fine("Answers");
    LOGGER.fine(dataSnapshot.toString());
    priorAnswers = dataSnapshot.getValue(new GenericTypeIndicator<List<Object>>() {});
    if (priorAnswers == null) {
        priorAnswers = new ArrayList<>();
    }
    updateUi();
}
 
Example #11
Source File: RxDatabaseReferenceTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
private <T> void callValueEventOnDataChange(GenericTypeIndicator<T> type, T value) {
    when(mockDataSnapshot.exists())
            .thenReturn(true);
    when(mockDataSnapshot.getValue(type))
            .thenReturn(value);

    valueEventListener.getValue().onDataChange(mockDataSnapshot);
}
 
Example #12
Source File: RxFirebaseDatabaseTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
private <T> void callValueEventOnDataChange(GenericTypeIndicator<T> type, T value) {
    when(mockDataSnapshot.exists())
            .thenReturn(true);
    when(mockDataSnapshot.getValue(type))
            .thenReturn(value);

    valueEventListener.getValue().onDataChange(mockDataSnapshot);
}
 
Example #13
Source File: RxFirebaseDatabaseTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataOfGenericTypeIndicator_Query() {
    List<String> values = new ArrayList<>();
    values.add("Foo");
    values.add("Bar");

    GenericTypeIndicator<List<String>> typeIndicator =
            new GenericTypeIndicator<List<String>>() {
            };

    TestObserver<List<String>> sub = TestObserver.create();

    RxFirebaseDatabase.dataOf(mockQuery, typeIndicator)
            .subscribe(sub);

    verifyQueryAddListenerForSingleValueEvent();
    callValueEventOnDataChange(typeIndicator, values);

    sub.assertComplete();
    sub.assertNoErrors();

    sub.assertValue(new Predicate<List<String>>() {
        @Override
        public boolean test(List<String> value) throws Exception {
            return "Foo".equals(value.get(0))
                    && "Bar".equals(value.get(1));
        }
    });

    sub.dispose();

    callValueEventOnDataChange(typeIndicator, values);

    // Ensure no more values are emitted after unsubscribe
    sub.assertValueCount(1);
}
 
Example #14
Source File: RxFirebaseDatabaseTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataChangesOfGenericTypeIndicator_Query() {
    List<String> values = new ArrayList<>();
    values.add("Foo");
    values.add("Bar");

    GenericTypeIndicator<List<String>> typeIndicator =
            new GenericTypeIndicator<List<String>>() {
            };

    TestObserver<DataValue<List<String>>> sub = TestObserver.create();

    RxFirebaseDatabase.dataChangesOf(mockQuery, typeIndicator)
            .subscribe(sub);

    verifyQueryAddValueEventListener();
    callValueEventOnDataChange(typeIndicator, values);

    sub.assertNotComplete();

    sub.assertValue(new Predicate<DataValue<List<String>>>() {
        @Override
        public boolean test(DataValue<List<String>> value) throws Exception {
            return value.isPresent()
                    && "Foo".equals(value.get().get(0))
                    && "Bar".equals(value.get().get(1));
        }
    });

    sub.dispose();

    callValueEventOnDataChange(typeIndicator, values);

    // Ensure no more values are emitted after unsubscribe
    sub.assertValueCount(1);
}
 
Example #15
Source File: RxFirebaseDatabaseTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataOfGenericTypeIndicator_DataReference() {
    List<String> values = new ArrayList<>();
    values.add("Foo");
    values.add("Bar");

    GenericTypeIndicator<List<String>> typeIndicator =
            new GenericTypeIndicator<List<String>>() {
            };

    TestObserver<List<String>> sub = TestObserver.create();

    RxFirebaseDatabase.dataOf(mockDatabaseReference, typeIndicator)
            .subscribe(sub);

    verifyDataReferenceAddListenerForSingleValueEvent();
    callValueEventOnDataChange(typeIndicator, values);

    sub.assertComplete();
    sub.assertNoErrors();

    sub.assertValue(new Predicate<List<String>>() {
        @Override
        public boolean test(List<String> value) throws Exception {
            return "Foo".equals(value.get(0))
                    && "Bar".equals(value.get(1));
        }
    });

    sub.dispose();

    callValueEventOnDataChange(typeIndicator, values);

    // Ensure no more values are emitted after unsubscribe
    sub.assertValueCount(1);
}
 
Example #16
Source File: RxFirebaseDatabaseTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataChangesOfGenericTypeIndicator_DataReference() {
    List<String> values = new ArrayList<>();
    values.add("Foo");
    values.add("Bar");

    GenericTypeIndicator<List<String>> typeIndicator =
            new GenericTypeIndicator<List<String>>() {
            };

    TestObserver<DataValue<List<String>>> sub = TestObserver.create();

    RxFirebaseDatabase.dataChangesOf(mockDatabaseReference, typeIndicator)
            .subscribe(sub);

    verifyDataReferenceAddValueEventListener();
    callValueEventOnDataChange(typeIndicator, values);

    sub.assertNotComplete();

    sub.assertValue(new Predicate<DataValue<List<String>>>() {
        @Override
        public boolean test(DataValue<List<String>> value) throws Exception {
            return value.isPresent()
                    && "Foo".equals(value.get().get(0))
                    && "Bar".equals(value.get().get(1));
        }
    });

    sub.dispose();

    callValueEventOnDataChange(typeIndicator, values);

    // Ensure no more values are emitted after unsubscribe
    sub.assertValueCount(1);
}
 
Example #17
Source File: RxFirebaseDatabase.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @param query
 * @param typeIndicator
 * @param <T>
 * @return
 */
@NonNull
@CheckReturnValue
public static <T> Single<T> dataOf(
        @NonNull Query query, @NonNull GenericTypeIndicator<T> typeIndicator) {
    return data(query).compose(new SingleTransformerOfGenericTypeIndicator<T>(typeIndicator));
}
 
Example #18
Source File: RxFirebaseDatabase.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @param ref
 * @param typeIndicator
 * @param <T>
 * @return
 */
@NonNull
@CheckReturnValue
public static <T> Single<T> dataOf(
        @NonNull DatabaseReference ref, @NonNull GenericTypeIndicator<T> typeIndicator) {
    return data(ref).compose(new SingleTransformerOfGenericTypeIndicator<T>(typeIndicator));
}
 
Example #19
Source File: RxFirebaseDatabase.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @param query
 * @param typeIndicator
 * @param <T>
 * @return
 */
@NonNull
@CheckReturnValue
public static <T> Observable<DataValue<T>> dataChangesOf(
        @NonNull Query query, @NonNull GenericTypeIndicator<T> typeIndicator) {
    return dataChanges(query)
            .compose(new TransformerOfGenericTypeIndicator<T>(typeIndicator));
}
 
Example #20
Source File: RxFirebaseDatabase.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @param ref
 * @param typeIndicator
 * @param <T>
 * @return
 */
@NonNull
@CheckReturnValue
public static <T> Observable<DataValue<T>> dataChangesOf(
        @NonNull DatabaseReference ref, @NonNull GenericTypeIndicator<T> typeIndicator) {
    return dataChanges(ref)
            .compose(new TransformerOfGenericTypeIndicator<T>(typeIndicator));
}
 
Example #21
Source File: ClusterMapContributeActivity.java    From intra42 with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    GenericTypeIndicator<HashMap<String, Cluster>> t = new GenericTypeIndicator<HashMap<String, Cluster>>() {
    };
    HashMap<String, Cluster> data = dataSnapshot.getValue(t);

    if (data != null) {
        clusters = new ArrayList<>(data.values());
        Collections.sort(clusters);
    }

    refresh();
}
 
Example #22
Source File: SingleTransformerOfGenericTypeIndicator.java    From rxfirebase with Apache License 2.0 4 votes vote down vote up
public SingleTransformerOfGenericTypeIndicator(@NonNull GenericTypeIndicator<T> indicator) {
    this.typeIndicator = indicator;
}
 
Example #23
Source File: TransformerOfGenericTypeIndicator.java    From rxfirebase with Apache License 2.0 4 votes vote down vote up
public TransformerOfGenericTypeIndicator(@NonNull GenericTypeIndicator<T> indicator) {
    this.typeIndicator = indicator;
}
 
Example #24
Source File: DataSnapshotMapper.java    From RxFirebase with Apache License 2.0 4 votes vote down vote up
public GenericTypedDataSnapshotMapper(GenericTypeIndicator<U> genericTypeIndicator) {
    this.genericTypeIndicator = genericTypeIndicator;
}
 
Example #25
Source File: DataSnapshotMapper.java    From RxFirebase with Apache License 2.0 4 votes vote down vote up
public static <U> DataSnapshotMapper<DataSnapshot, U> of(GenericTypeIndicator<U> genericTypeIndicator) {
    return new GenericTypedDataSnapshotMapper<U>(genericTypeIndicator);
}