Android Open Source - opentraining Workout






From Project

Back to project page opentraining.

License

The source code is released under:

GNU General Public License

If you think the Android project opentraining 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

/**
 * //w  w  w . java2  s .  c  om
 * This is OpenTraining, an Android application for planning your your fitness training.
 * Copyright (C) 2012-2014 Christian Skubich
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package de.skubware.opentraining.basic;

import java.io.Serializable;
import java.util.*;

import android.util.Log;

/**
 * This class represents a single workout. A workout needs a name and consists
 * out of more than one {@code FitnessExercise}s.
 * 
 * It is possible to iterate through the {@code FitnessExercises} and to add new
 * ones.
 * 
 * @author Christian Skubich
 * 
 */
public class Workout implements Iterable<FitnessExercise>, Serializable {
  /** Default serialVersionUID */
  private static final long serialVersionUID = 1L;

  /** Tag for logging */
  static final String TAG = "Workout";

  public static int DEFAULT_EMPTYROWS = 8;

  private String name;
  private int emptyRows = DEFAULT_EMPTYROWS;
  private ArrayList<FitnessExercise> fitnessExercises = new ArrayList<FitnessExercise>();

  /**
   * Constructor of this class.
   * 
   * @param name
   *            The name of the workout
   * @param fExes
   *            The FitnessExercises of the workout
   * 
   * @throws NullPointerException
   *             if any argument is null or empty
   * 
   */
  public Workout(String name, Collection<FitnessExercise> fExes) {
    if (name == null || name.replaceAll(" ", "").equals("") || fExes == null || fExes.isEmpty() || fExes.contains(null)) {
      throw new NullPointerException();
    }

    // Assign given values
    this.name = name;
    this.fitnessExercises = new ArrayList<FitnessExercise>();
    this.fitnessExercises.addAll(fExes);
  }

  /**
   * @see #Workout(String, Collection<FitnessExercise>)
   */
  public Workout(String name, FitnessExercise... fExes) {
    this(name, Arrays.asList(fExes));
  }

  /**
   * Alternative constructor of this class for given ExerciseTypes. Have a
   * look at @see #Workout(String, Collection<FitnessExercise>) for more
   * information.
   * 
   * @param name
   *            The name of the workout
   * @param exes
   *            The ExerciseTypes that should be added to workout.
   */
  public Workout(String name, List<ExerciseType> exes) {
    this(name, ExerciseType.asFitnessExercise(exes));
  }

  /** {@inheritDoc} */
  public Iterator<FitnessExercise> iterator() {
    return this.fitnessExercises.iterator();
  }

  /**
   * @see java.util.List#contains(Object)
   */
  public boolean contains(FitnessExercise fEx) {
    return this.fitnessExercises.contains(fEx);
  }

  /** Adds a new TrainingEntriy to each FitnessExercise in this Workout. */
  public void addTrainingEntry(Date date) {
    for (FitnessExercise fEx : this.fitnessExercises) {
      fEx.addTrainingEntry(date);
    }
  }

  /**
   * Returns a set of dates, for which {@link TrainingEntry}s do exist.
   * 
   * @return A set of dates, for which training entries do exist.
   */
  /*
   * public Set<Date> getTrainingEntryDates(){ List<Date> dateList = new
   * ArrayList<Date>(); for(FitnessExercise fEx:this.fitnessExercises){
   * for(TrainingEntry e:fEx.getTrainingEntryList()){
   * dateList.add(e.getDate()); } }
   * 
   * // assert that number dates is correct Set<Date> dateSet = new
   * HashSet<Date>(dateList); if( dateList.size() != (dateSet.size() *
   * this.fitnessExercises.size()) ){ throw new
   * AssertionError("Incorrect number of TrainingEntries. This should not happen."
   * ); } return dateSet; }
   */

  /**
   * Checks if there are TrainingEntries for the FitnessExercises.
   * 
   * @return True if the FitnessExercises have at least one TrainingEntry
   */
  public boolean hasTrainingEntries() {
    return this.fitnessExercises.get(0).getTrainingEntryList().size() > 0;
  }

  /**
   * Getter for name
   * 
   * @return The name of the workout
   */
  public String getName() {
    return this.name;
  }

  /**
   * Setter for name
   * 
   * @param name
   *            The new name of the workout
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Getter for the {@code FitnessExercises}
   * 
   * @return A list with the FitnessExercises
   */
  public List<FitnessExercise> getFitnessExercises() {
    return this.fitnessExercises;
  }

  /**
   * Adds the given {@code FitnessExercise} to the Workout.
   * 
   * @param fEx
   *            The FitnessExercise to add
   * 
   * @throws IllegalArgumentException
   *             if there is already an FitnessExercise with the same
   *             ExerciseType
   */
  public void addFitnessExercise(FitnessExercise fEx) {
    ExerciseType exType = fEx.getExType();
    for (FitnessExercise ex : this.getFitnessExercises()) {
      if (ex.getExType().equals(exType))
        throw new IllegalArgumentException("There is already an Exercise with the ExerciseType: " + exType.toString());
    }

    this.fitnessExercises.add(fEx);
  }

  /**
   * Removes the given {@code FitnessExercise} from the Workout.
   * 
   * @param fEx
   *            The FitnessExercise to remove
   */
  public void removeFitnessExercise(FitnessExercise fEx) {
    this.fitnessExercises.remove(fEx);
  }

  /**
   * Updates the {@link FitnessExercise}.
   * 
   * @param changedFEx
   *            The FitnessExercise that has changed
   * 
   * @throws IllegalArgumentException
   *             if the Exercise is not contained in this Workout
   */
  public void updateFitnessExercise(FitnessExercise changedFEx) {
    Log.d(TAG, "updateFitnessExercise(), changedFEx: " + changedFEx.toDebugString());
    FitnessExercise oldFex = null;
    for (FitnessExercise fEx : this.fitnessExercises) {
      // this comparison relies on the fact that each Workout can only
      // contain each ExerciseType once
      if (fEx.getExType().equals(changedFEx.getExType())) {
        oldFex = changedFEx;
        int oldIndex = fitnessExercises.indexOf(fEx);
        fitnessExercises.remove(oldIndex);
        fitnessExercises.add(oldIndex, changedFEx);
        break;
      }
    }

    if (oldFex == null) {
      throw new IllegalArgumentException("FitnessExercise: " + changedFEx.toString() + " is not contained in this Workout: "
          + this.toString());
    }

  }

  /** {@inheritDoc} */
  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Workout))
      return false;
    Workout w = (Workout) o;

    // hashCode() check
    if (this.hashCode() != w.hashCode())
      return false;

    // name check
    if (!w.getName().equals(this.getName()))
      return false;

    // FitnessExercise check
    if (!w.getFitnessExercises().containsAll(this.fitnessExercises))
      return false;
    if (!this.fitnessExercises.containsAll(w.getFitnessExercises()))
      return false;

    return true;
  }

  /** {@inheritDoc} */
  @Override
  public int hashCode() {
    return this.name.hashCode() + this.fitnessExercises.size();
  }

  /** {@inheritDoc} */
  @Override
  public String toString() {
    return this.name;
  }

  /**
   * Returns a String that represents this object. Should only be used for
   * debugging.
   * 
   * @return A String that represents this object.
   */
  public String toDebugString() {
    StringBuilder builder = new StringBuilder();

    builder.append("Name: " + name + "\n");
    builder.append("Empty Rows: " + emptyRows + "\n");
    for (FitnessExercise fEx : getFitnessExercises()) {
      builder.append("\n" + fEx.toDebugString());
    }

    return builder.toString();
  }

  public void switchExercises(FitnessExercise first, FitnessExercise second) {
    if (!this.fitnessExercises.contains(first) || !this.fitnessExercises.contains(second)) {
      throw new AssertionError("FitnessExercise does not exist in workout");
    }

    int idxFirst = this.fitnessExercises.indexOf(first);
    int idxSecond = this.fitnessExercises.indexOf(second);

    Collections.swap(fitnessExercises, idxFirst, idxSecond);

  }

  /**
   * Getter for emptyRows
   * 
   * @return The number of empty rows (>0)
   */
  public int getEmptyRows() {
    return this.emptyRows;
  }

  /**
   * Setter for emptyRows
   * 
   * @param emptyRows
   *            New number of empty rows, must be positive
   * 
   * @throws IllegalArgumentException
   *             if argument is below 0
   */
  public void setEmptyRows(int emptyRows) {
    if (emptyRows > 0)
      this.emptyRows = emptyRows;
    else
      throw new IllegalArgumentException("There must be more than 0 empty rows");

    Log.d(TAG, "setEmptyRows() to " + this.emptyRows);
  }

  /**
    * Returns a new Workout only containing the Exercises, but no history(FSets).
    * The crated Workout will have the same name.
    */
  public Workout getWorkoutWithoutHistory(){
    List<FitnessExercise> fExList = new ArrayList<FitnessExercise>();
    for(FitnessExercise fEx: this.fitnessExercises){
      fExList.add(new FitnessExercise(fEx.getExType())); // copy exercises without FSets
    }
    Workout w = new Workout(this.name, fExList);
    return w;
  }
}




Java Source Code List

at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter.java
at.technikum.mti.fancycoverflow.FancyCoverFlowItemWrapper.java
at.technikum.mti.fancycoverflow.FancyCoverFlow.java
de.skubware.opentraining.activity.ChangeLogDialog.java
de.skubware.opentraining.activity.DisclaimerDialog.java
de.skubware.opentraining.activity.MainActivity.java
de.skubware.opentraining.activity.NavigationGalleryAdapter.java
de.skubware.opentraining.activity.SelectWorkoutDialogFragment.java
de.skubware.opentraining.activity.WhatsNewDialog.java
de.skubware.opentraining.activity.acra.ACRACrashReportMailer.java
de.skubware.opentraining.activity.acra.ACRAFeedbackMailer.java
de.skubware.opentraining.activity.acra.OpenTrainingApplication.java
de.skubware.opentraining.activity.acra.RequestExerciseUpdate.java
de.skubware.opentraining.activity.create_exercise.CreateExerciseActivity.java
de.skubware.opentraining.activity.create_exercise.CustomSpinner.java
de.skubware.opentraining.activity.create_exercise.DescriptionFragment.java
de.skubware.opentraining.activity.create_exercise.EditImageMetadataDialog.java
de.skubware.opentraining.activity.create_exercise.EquipmentDataFragment.java
de.skubware.opentraining.activity.create_exercise.ExerciseImageListAdapter.java
de.skubware.opentraining.activity.create_exercise.ImageFragment.java
de.skubware.opentraining.activity.create_exercise.MuscleDataFragment.java
de.skubware.opentraining.activity.create_exercise.NameFragment.java
de.skubware.opentraining.activity.create_exercise.SimpleDataFragment.java
de.skubware.opentraining.activity.create_exercise.SpinnerDataFragment.java
de.skubware.opentraining.activity.create_workout.DialogFilterMusclesAndEquipment.java
de.skubware.opentraining.activity.create_workout.DialogWorkoutOverviewFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseDetailOnGestureListener.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListAdapter.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListFragment.java
de.skubware.opentraining.activity.create_workout.SelectMuscleDialog.java
de.skubware.opentraining.activity.create_workout.SendExerciseFeedbackDialogFragment.java
de.skubware.opentraining.activity.create_workout.upload_exercise.ExerciseImage.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseImagesAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.WgerRestService.java
de.skubware.opentraining.activity.manage_workouts.RenameWorkoutDialogFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListFragment.java
de.skubware.opentraining.activity.settings.LicenseDialog.java
de.skubware.opentraining.activity.settings.SettingsActivity.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncResultReceiver.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncService.java
de.skubware.opentraining.activity.settings.sync.RestClient.java
de.skubware.opentraining.activity.settings.sync.SyncFinishedDialog.java
de.skubware.opentraining.activity.settings.sync.WgerImageDownloader.java
de.skubware.opentraining.activity.settings.sync.WgerJSONParser.java
de.skubware.opentraining.activity.start_training.DialogFragmentAddEntry.java
de.skubware.opentraining.activity.start_training.DialogFragmentTrainingEntryTable.java
de.skubware.opentraining.activity.start_training.FExDetailActivity.java
de.skubware.opentraining.activity.start_training.FExDetailFragment.java
de.skubware.opentraining.activity.start_training.FExListActivity.java
de.skubware.opentraining.activity.start_training.FExListAdapter.java
de.skubware.opentraining.activity.start_training.FExListFragment.java
de.skubware.opentraining.activity.start_training.RecoveryTimerManager.java
de.skubware.opentraining.activity.start_training.SwipeDismissListViewTouchListener.java
de.skubware.opentraining.activity.start_training.SwipeDismissTouchListener.java
de.skubware.opentraining.activity.start_training.TrainingEntryListAdapter.java
de.skubware.opentraining.basic.ActivationLevel.java
de.skubware.opentraining.basic.ExerciseTag.java
de.skubware.opentraining.basic.ExerciseType.java
de.skubware.opentraining.basic.FSet.java
de.skubware.opentraining.basic.FitnessExercise.java
de.skubware.opentraining.basic.IExercise.java
de.skubware.opentraining.basic.License.java
de.skubware.opentraining.basic.Muscle.java
de.skubware.opentraining.basic.SportsEquipment.java
de.skubware.opentraining.basic.TrainingEntry.java
de.skubware.opentraining.basic.Translatable.java
de.skubware.opentraining.basic.Workout.java
de.skubware.opentraining.db.Cache.java
de.skubware.opentraining.db.DataHelper.java
de.skubware.opentraining.db.DataProvider.java
de.skubware.opentraining.db.IDataProvider.java
de.skubware.opentraining.db.parser.AbstractJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTagJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTypeXMLParser.java
de.skubware.opentraining.db.parser.IParser.java
de.skubware.opentraining.db.parser.MuscleJSONParser.java
de.skubware.opentraining.db.parser.SportsEquipmentJSONParser.java
de.skubware.opentraining.db.parser.WorkoutXMLParser.java
de.skubware.opentraining.db.parser.XMLSaver.java
de.skubware.opentraining.db.rest.ExerciseImageGSONSerializer.java
de.skubware.opentraining.db.rest.ExerciseTypeGSONSerializer.java
de.skubware.opentraining.db.rest.LanguageGSONDeserializer.java
de.skubware.opentraining.db.rest.MuscleGSONDeserializer.java
de.skubware.opentraining.db.rest.ServerModel.java
de.skubware.opentraining.db.rest.SportsEquipmentGSONDeserializer.java
de.skubware.opentraining.exporter.HTMLExporter.java
de.skubware.opentraining.exporter.LaTeXExporter.java
de.skubware.opentraining.exporter.WorkoutExporter.java