Difference between revisions of "Solução: Problema da memória transacional (Diogo Maron)"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(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...)
 
Line 1: Line 1:
<code land="java">
 
 
/*
 
* To change this template, choose Tools | Templates
 
* and open the template in the editor.
 
*/
 
 
 
package console;
 
package console;
  
Line 11: Line 4:
 
import memoriatransacional.MemoriaTransacional;
 
import memoriatransacional.MemoriaTransacional;
  
/**
 
*
 
* @author dmaron
 
*/
 
 
public class Main {
 
public class Main {
  
Line 35: Line 24:
 
}
 
}
  
 
/*
 
* To change this template, choose Tools | Templates
 
* and open the template in the editor.
 
*/
 
  
 
package memoriatransacional;
 
package memoriatransacional;
  
/**
 
*
 
* @author dmaron
 
*/
 
 
public class MemoriaTransacional {
 
public class MemoriaTransacional {
 
      
 
      
Line 123: Line 103:
 
}
 
}
  
 
/*
 
* To change this template, choose Tools | Templates
 
* and open the template in the editor.
 
*/
 
  
 
package memoriatransacional;
 
package memoriatransacional;
Line 133: Line 108:
 
import java.util.ArrayList;
 
import java.util.ArrayList;
  
/**
 
*
 
* @author dmaron
 
*/
 
 
public class Transaction {
 
public class Transaction {
 
     private ArrayList<String> ioEscritas = new ArrayList<String>();
 
     private ArrayList<String> ioEscritas = new ArrayList<String>();

Revision as of 13:32, 3 June 2009

package console;

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

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]);
       }
   }

}


package memoriatransacional;

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] = "";
   }

}


package memoriatransacional;

import java.util.ArrayList;

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;
   }

}


</code>