dunque, questa è una classe tipo quella che devo gestirecodice:public class CestoFrutta { public static final String ENCODING = "IBM037"; private String tipoCestino = ""; //1 private Number numeroMele=new Long(0l); //2 private Number numerPere=new Integer(0); //3 private String coloreCestino = ""; //4 //inizio lunghezze private int l_coloreCestino=48; private int l_tipoCestino=24; private int l_numeroMele=12; private int l_numeroPere=8; //fine lunghezze public CestoFrutta() { super(); tipoCestino = init(l_tipoCestino); coloreCestino= init(l_coloreCestino); } public byte[] getBytes() throws Throwable { StringBuffer bf = new StringBuffer(this.getTipoCestino());//1 bf.append(String.format("%12d", this.getNumeroMele()));//2 bf.append(String.format("%8d", this.getNumerPere()));//3 bf.append(this.getColoreCestino());//4 return bf.toString().getBytes(ENCODING); } public void setBytes(byte[] array) throws Throwable { byte[] array_tipoCestino = new byte[l_tipoCestino]; byte[] array_numeroMele = new byte[l_numeroMele]; byte[] array_numeroPere= new byte[l_numeroPere]; byte[] array_coloreCestino = new byte[l_coloreCestino]; int j = 0; //1 tipoCestino=setStr(tipoCestino, j,array_tipoCestino, array); j += array_tipoCestino.length; //2 numeroMele=setNum(numeroMele, j,array_numeroMele, array); j += array_numeroMele.length; //3 numerPere=setNum(numerPere, j,array_numeroPere, array); j += array_numeroPere.length; //4 coloreCestino=setStr(coloreCestino, j,array_coloreCestino, array); j += array_coloreCestino.length; } private Number setNum(Number num, int j, byte[] arr1, byte[] arr2) throws UnsupportedEncodingException, ParseException{ for (int i = 0; i < arr1.length; i++) { arr1[i] = arr2[j + i]; } String str = new String(arr1, ENCODING); num = NumberFormat.getInstance().parse(str.trim()); return num; } private String setStr(String str, int j, byte[] arr1, byte[] arr2) throws UnsupportedEncodingException, ParseException{ for (int i = 0; i < arr1.length; i++) { arr1[i] = arr2[j + i]; } str = new String(arr1, ENCODING); return str; } private String init(int len) { StringBuffer bf = new StringBuffer(""); for (int i = 0; i < len; i++) { bf.append(" "); } return bf.toString(); } ...tutti i get e i set dei campi }
in sostanza il mio problema è: se guardi la getBytes e la setBytes, noterai (lo vedi dai commenti) che c'è un ordine di costruzione all'interno degli stessi metodi, che riguarda i 4 campi --- ora i campi sono 4 ma potrebbero diventare 20, e magari avrei bisogno di cambiare l'ordine --- per come è fatto il codice ora dovrei modificarlo in questa maniera (esempio nella getBytes):
prima:
codice:StringBuffer bf = new StringBuffer(this.getTipoCestino());//1 bf.append(String.format("%12d", this.getNumeroMele()));//2 bf.append(String.format("%8d", this.getNumerPere()));//3 bf.append(this.getColoreCestino());//4 return bf.toString().getBytes(ENCODING);cambiando quindi la sequenza --- mi piacerebbe invece avere questi elementi legati a dei numeri e andare a toccare solo questi numeri rappresentanti l'ordine; si potrebbe fare con le liste o le mappe ma forse c'è di megliocodice:dopo (inverto l'ordine tra mele e pere): StringBuffer bf = new StringBuffer(this.getTipoCestino());//1 bf.append(String.format("%8d", this.getNumerPere()));//2 bf.append(String.format("%12d", this.getNumeroMele()));//3 bf.append(this.getColoreCestino());//4 return bf.toString().getBytes(ENCODING);
grazie

Rispondi quotando