Android Open Source - meeting-app Rank By






From Project

Back to project page meeting-app.

License

The source code is released under:

Apache License

If you think the Android project meeting-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Google Inc./*from   w w w . j  a  va  2s . c  om*/
 *
 * 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.
 */

package com.example.wrappers;

import java.util.List;
import java.util.ArrayList;

/**
 * RankBy class, used to rank results based on the weighting of particular criteria.
 */
public class RankBy {
  private List<PlaceResult> results;
  private PlaceCriteria criteria;
  private double total;
  private List<Double> ranks;

  /**
   * Constructor method sets up required fields. 
   * @param c the PlaceCriteria instance containing weighting information. 
   * @param r The list of results to be ranked. 
   */
  public RankBy (PlaceCriteria c, List<PlaceResult> r) {
    results = r;
    criteria = c;

    ranks = new ArrayList<Double> ();

    total = criteria.rating + criteria.photo + criteria.openNow + 
      criteria.price + criteria.types;
  }

  /** 
   * Go through a list of places and give each a ranking value.
   */
  void setPlaceRank () {
    int hasPhoto;
    int open;

    for (PlaceResult result: results) {
      try {
        hasPhoto = (result.photos.size () != 0)? 1:0;
      } catch (NullPointerException e) {
        hasPhoto = 0;
      }

      try {
        open = (result.opening_hours.open_now == true)? 1:0;
      } catch (NullPointerException e) {
        open = 0;
      }
      double priceRank = 1-(result.price_level/4.0);
      int hasType = hasType (result.types);

      ranks.add((criteria.rating * (result.rating / 5) + open * criteria.openNow + 
                 criteria.photo * hasPhoto + criteria.price * priceRank + 
                 criteria.types * hasType) / total);
    }
  }

  /**
   * Determine whether a place matches any of the types specified in the PlaceCriteria.
   */
  int hasType (String[] types) {
    if (criteria.typesList.size () == 0) {
      return 0;
    }
    for (String type: types) {
      if (criteria.typesList.contains(type)) {
        return 1;
      }
    }
    return 0;
  }

  /**
   * Rank the places and return the top number of results.
   * @param number how many results to return. 
   * If there are less total results than number then return all results. 
   * @returns a list of sorted results of length number.
   */
  public List<PlaceResult> rankPlaces (int number) {
    setPlaceRank ();
    return getBest(number);
  }

  /**
   * Get the best number of results and return them sorted.
   */
  List<PlaceResult> getBest (int number) {
    number=Math.min(number, results.size ());
    List<PlaceResult> bestResults = new ArrayList<PlaceResult> ();
    // A simple and inefficient approach, but get something working.
    double maxRank=0;
    int bestResult=0;

    for (int i=0; i < number; i++) {
      for (int j =0; j < results.size (); j++) {
        if (ranks.get(j) > maxRank) {
          maxRank=ranks.get(j);
          bestResult=j;
        }
      }

      bestResults.add(results.remove(bestResult));
      ranks.remove(bestResult);
      maxRank=0;
      bestResult=0;
    }
    return bestResults;
  }
}




Java Source Code List

com.example.geocodeservice.GeocodeResponse.java
com.example.geocodeservice.GeocodeResult.java
com.example.geocodeservice.GeocodeServiceTest.java
com.example.geocodeservice.GeocodeService.java
com.example.meetingapp.LocationParcel.java
com.example.meetingapp.MainActivity.java
com.example.meetingapp.MapResultsFragment.java
com.example.meetingapp.ModifyStateCallback.java
com.example.meetingapp.OptionsFragment.java
com.example.meetingapp.PickCategoryFragment.java
com.example.meetingapp.PickLocationFragment.java
com.example.meetingapp.PlacesAutocompleteAdapter.java
com.example.meetingapp.ResultAdapter.java
com.example.meetingapp.ShowDetailsFragment.java
com.example.meetingapp.ShowResultsFragment.java
com.example.meetingapp.State.java
com.example.placedetails.DetailsResponse.java
com.example.placedetails.DetailsResult.java
com.example.placedetails.DetailsService.java
com.example.placephotos.PhotoService.java
com.example.wrappers.DistanceMatrixResponse.java
com.example.wrappers.DistanceMatrixTest.java
com.example.wrappers.DistanceMatrix.java
com.example.wrappers.LatLng.java
com.example.wrappers.PlaceAutocompletePrediction.java
com.example.wrappers.PlaceCriteria.java
com.example.wrappers.PlaceQuery.java
com.example.wrappers.PlaceResult.java
com.example.wrappers.PlaceSearch.java
com.example.wrappers.PlaceWrapperTest.java
com.example.wrappers.PlaceWrapper.java
com.example.wrappers.PlacesAutocompleteResponse.java
com.example.wrappers.PlacesAutocomplete.java
com.example.wrappers.PlacesResponse.java
com.example.wrappers.PlacesServiceTest.java
com.example.wrappers.PlacesService.java
com.example.wrappers.RankBy.java
com.example.wrappers.StaticMapTest.java
com.example.wrappers.StaticMap.java