Solução: Problema da memória transacional (Diogo Maron)

From AdonaiMedrado.Pro.Br
Revision as of 13:31, 3 June 2009 by 200.17.147.2 (Talk) (New page: <code land="java"> * To change this template, choose Tools | Templates * and open the template in the editor.: package console; import java.util.Scanner; import memoriatransacio...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package console;

import java.util.Scanner; import memoriatransacional.MemoriaTransacional;

/**

*
* @author dmaron
*/

public class Main {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       // TODO code application logic here
       Scanner loScanner = new Scanner(System.in);
       String lsEntrada = loScanner.nextLine();
       MemoriaTransacional loMemoria = new MemoriaTransacional();
       String[] loSaida = loMemoria.ExecutarEscrita(lsEntrada.split(" "));
       for (int i = 0; i < loSaida.length; i++){
           String lsLetra = new String();
           lsLetra +=(char)((int)'A' + i);
           System.out.println(lsLetra + "=" + loSaida[i]);
       }
   }

}


/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package memoriatransacional;

/**

*
* @author dmaron
*/

public class MemoriaTransacional {

   private int TAMANHO_MEMORIA = 26;
   
   private String[] ioMemoria = new String[TAMANHO_MEMORIA];
   private Transaction ioTransaction;
   
   public MemoriaTransacional() {
       this.Reset();
   }
   public String[] ExecutarEscrita(String[] aoEscritas){
       for (int i = 0; i<aoEscritas.length; i++){
           if (aoEscritas[i].contains("="))
               this.ExecutarEscrita(aoEscritas[i]);
           else {
               this.ExecutarTransaction(aoEscritas[i]);
           }
       }
       return ioMemoria;
   }
   
   public void ExecutarEscrita(String asEscrita){
       if (this.IsInTransaction())
           this.ioTransaction.AddEscrita((asEscrita));
       else
           this.Escrever(asEscrita);
   }
   
   private void Escrever(String asEscrita){
       String[] loValores = asEscrita.split("=");
       int liPosicao = (int)loValores[0].charAt(0) - (int)'A';
       this.ioMemoria[liPosicao] = loValores[1];
   }
   
   private void ExecutarTransaction(String asComando){
       if (asComando.equals("B"))
           this.BeginTransaction();
       else if (asComando.equals("C"))
           this.CommitTransaction();
       else if (asComando.equals("R"))
           this.RollBackTransaction();
   }
   
   private void BeginTransaction() {
       if (this.IsInTransaction()){
           this.ioTransaction.Begin();
       }
       else
           this.ioTransaction = new Transaction();
   }
   
   private void CommitTransaction(){
       if (this.ioTransaction.Commit()) {
           for (String loString : this.ioTransaction.GetEscritas())
               this.Escrever(loString);
           this.ioTransaction=null;
       }
   }
   
   private void RollBackTransaction(){
       if (this.ioTransaction.RollBack()) {
           this.ioTransaction=null;
       }
   }
   
   private Boolean IsInTransaction(){
       return (this.ioTransaction != null);
   }
   
   public void Reset(){
       for (int i =0; i < TAMANHO_MEMORIA; i++)
           this.ioMemoria[i] = "";
   }

}


/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package memoriatransacional;

import java.util.ArrayList;

/**

*
* @author dmaron
*/

public class Transaction {

   private ArrayList<String> ioEscritas = new ArrayList<String>();
   private Transaction ioInner;
   private Boolean ibOpen = true;
   
   public void AddEscrita(String asEscrita){
       if (this.HasInner())
           this.ioInner.AddEscrita(asEscrita);
       else
           this.ioEscritas.add(asEscrita);
   }
   
   public ArrayList<String> GetEscritas(){
       return this.ioEscritas;
   }
   
   private Boolean HasInner(){
       return (this.ioInner != null);
   }
   
   public void Begin(){
       if (this.HasInner()) {
           this.ioInner.Begin();
       }
       else {
           this.ioInner = new Transaction();
       }
   }
   
   public Boolean Commit(){
       if (this.HasInner()){
           if (this.ioInner.Commit()){
               ArrayList<String> loEscritasInner = this.ioInner.GetEscritas();
               this.ioInner = null;
               for (String lsEscrita : loEscritasInner)
                   this.AddEscrita(lsEscrita);
           }
       }
       else {
           this.ibOpen = false;
       }
       return !this.ibOpen;
   }
   
   public Boolean RollBack() {
       if (!this.HasInner()) {
           this.ibOpen = false;
       }
       else {
           if (this.ioInner.RollBack())
               this.ioInner = null;
       }
       return !this.ibOpen;
   }

}