Skip to content

Instantly share code, notes, and snippets.

@haideralipunjabi
Created March 5, 2017 15:38
Show Gist options
  • Save haideralipunjabi/26b5031b5889845b86dc6ebc0b0aa31a to your computer and use it in GitHub Desktop.
Save haideralipunjabi/26b5031b5889845b86dc6ebc0b0aa31a to your computer and use it in GitHub Desktop.
The CalculateQualityIndexes implements a method that calculates the index for different sections of photo qualities, and returns them as an array.
/**
* <h1>Calculate Index of Photo Resolutions</h1>
* The CalculateQualityIndexes implements a method
* that calculates the index for different sections
* of photo qualities, and returns them as an array.
* <h2>Example usage:</h2>
* <p> Calculating the indexes for High, Medium and Low
* resolution photos fetched as JSONArray from Facebook
* GraphAPIs images field of photo endpoint,
* and stored in a LinkedHashMap.
* </p>
* <h2>Author's Remark</h2>
* <p>I have used this in my closed-source KPCExplorer
* Android App</p>
*
* @author haideralipunjabi
* @version 1.0
* @since 05-03-2017
*/
public class QualityUtils{
/**
* This method returns an int array containing the indexes
* for specified number of qualities divided into
* specified number of sections
*
* @param qualities This is the total number of qualities
* @param sections This is the total number of sections
* @return int[] This returns all the calculated indexes
* as an int array
*/
public static int[] CalculateQualityIndexes(int qualities, int sections) {
final int[] mIndexes = new int[sections];
final int[] mSizes = new int[sections];
final int mQuotient = qualities / sections;
final int mRemainder = qualities % sections;
for (int i = 0; i < sections; i++) {
int size = 0;
if (i <= mRemainder && i > 0) {
mSizes[i] = mQuotient + 1;
} else {
mSizes[i] = mQuotient;
}
mIndexes[i] = addSize(mSizes, i);
}
return mIndexes;
}
/**
* This method adds the size of sections present
* before the specified index
* @param sizes Array of all the calculated sizes
* @param index index of section before which the
* total size is to be calculated
* @return int The total sum of sizes
*/
private static int addSize(int[] sizes, int index) {
if (index == 0) {
return 0;
}
return sizes[index - 1] + addSize(sizes, index - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment