Android Open Source - MultiWork Conflict Checker






From Project

Back to project page MultiWork.

License

The source code is released under:

Copyright (c) 2013, Torti Andr?s Fernando All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions a...

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

package com.utils.andres;
//from  w w  w  . ja  va2s. c  o  m
import java.util.ArrayList;
import java.util.Map;

import android.content.SharedPreferences;

public class ConflictChecker {
  
  ArrayList<Dependency> list = new ArrayList<Dependency>();
  SharedPreferences mPreferences;
  
  /**
   * Detector de conflicto en base a las dependencias
   * @param mSharedPreferences preferencia a usar
   */
  public ConflictChecker(SharedPreferences mSharedPreferences) {
    mPreferences = mSharedPreferences;
  }
  
  public void addDependency (Dependency mDependency) {
    list.add(mDependency);
  }
  
  /**
   * Detecta conflictos y los repara en las preferencias, para que una dependencia se cumpla se debe cumplir la dependencia maestra
   * y las sub-dependencias, de otro modo se las anula con el valor especificado en cada dependencia
   * @return
   */
  public boolean detectConflicts () {
    
    boolean conflictsCorrected = false;
    
    for(Dependency mDependency : list){
      // Si el master key cumple con la dependencia
      String masterValue = mPreferences.getString(mDependency.getMasterKey(), null);
      
      if (masterValue != null && Integer.valueOf(masterValue) == mDependency.getMasterValue()) {
        for(Map.Entry<String, Pair<String, Integer>> entry : mDependency.getDependencyMap().entrySet()){
          String key = entry.getKey();
          String refKey = entry.getValue().getFirst();
          int value = entry.getValue().getSecond();
          
          if (refKey != null) {
            if(mPreferences.getString(refKey, null) == null){
              //throw new NullPointerException("El key de referencia no existe, asegrese de que exista");
                            invalidateDependency(mDependency, refKey, mDependency.getInvalidationValue());
                            conflictsCorrected = true;
                        }else{
              int refValue = Integer.valueOf(mPreferences.getString(refKey, null));
              String targetKey = key.replace("*", ""+refValue);

              try {
                if(Integer.valueOf(mPreferences.getString(targetKey, null)) != value){
                  invalidateDependency(mDependency, refKey, mDependency.getInvalidationValue());
                  conflictsCorrected = true;
                }
              } catch (NumberFormatException e) {
                invalidateDependency(mDependency, refKey, mDependency.getInvalidationValue());
                conflictsCorrected = true;
                            }
                        }
          }
          else{
            String keyString = mPreferences.getString(key, null);
            if(keyString != null && Integer.valueOf(keyString) != value){
              invalidateDependency(mDependency, null, mDependency.getInvalidationValue());
              conflictsCorrected = true;
            }
          }
        }
      }
    }
    return conflictsCorrected;
  }
  
  private void invalidateDependency (Dependency mDependency, String refKey, int invalidateValue){
    mPreferences.edit().putString(mDependency.getMasterKey(), ""+invalidateValue).apply();
        // Si no es null cambio directamente el valor del key de referencia
    if(refKey != null){
      mPreferences.edit().putString(refKey, ""+invalidateValue).apply();
    }
    
    for(String key : mDependency.getDependencyMap().keySet()){
      if(!key.contains("*")) mPreferences.edit().putString(key, ""+invalidateValue).apply();
    }
  }

}




Java Source Code List

com.bluetoothutils.andres.BTSingleSynchTransfer.java
com.bluetoothutils.andres.BluetoothHelper.java
com.bluetoothutils.andres.DeviceScanner.java
com.bluetoothutils.andres.OnBluetoothConnected.java
com.bluetoothutils.andres.OnDeviceSelected.java
com.bluetoothutils.andres.OnNewBluetoothDataReceived.java
com.frecuencimeter.andres.FrecView.java
com.frecuencimeter.andres.Frecuencia.java
com.multiwork.andres.ApplicationContext.java
com.multiwork.andres.LCView.java
com.multiwork.andres.MainMenu.java
com.multiwork.andres.MainPrefs.java
com.multiwork.andres.MenuListAdapter.java
com.protocolanalyzer.andres.AnalyzerExpandableAdapter.java
com.protocolanalyzer.andres.CRC16.java
com.protocolanalyzer.andres.LogicAnalizerPrefsFragment.java
com.protocolanalyzer.andres.LogicAnalizerPrefs.java
com.protocolanalyzer.andres.LogicAnalyzerActivity.java
com.protocolanalyzer.andres.LogicAnalyzerChartFragment.java
com.protocolanalyzer.andres.LogicAnalyzerListFragment.java
com.protocolanalyzer.andres.OnActionBarClickListener.java
com.protocolanalyzer.andres.OnDataClearedListener.java
com.protocolanalyzer.andres.OnDataDecodedListener.java
com.roboticarm.andres.BrazoRobot.java
com.roboticarm.andres.JoystickView.java
com.roboticarm.andres.Joystick.java
com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.java
com.tjerkw.slideexpandable.library.ActionSlideExpandableListView.java
com.tjerkw.slideexpandable.library.ExpandCollapseAnimation.java
com.tjerkw.slideexpandable.library.SlideExpandableListAdapter.java
com.tjerkw.slideexpandable.library.SlideExpandableListView.java
com.tjerkw.slideexpandable.library.WrapperListAdapterImpl.java
com.utils.andres.ConflictChecker.java
com.utils.andres.Dependency.java
com.utils.andres.Pair.java