Eva
Java粉絲募集中
  • 簡介
  • 我們的優勢
  • 最新消息
  • 下載與使用
  • 範例與說明
    • Eva Objects >
      • Bean類別定義
      • Entity型別定義
      • Struct型別定義
      • 建立Entity空白物件
      • 建立Entity修改物件
      • 建立Entity條件物件
      • 建立Struct值物件
    • Eva_ORM >
      • ORM程式主體
      • 使用條件物件執行操作
      • 執行更複雜條件的操作.
      • 分頁取回資料
      • 操作JDBC的參數
      • Array Property的操作
      • Collection Property的操作
      • 更複雜的Collection Property
      • 指定表格名稱
      • 新增資料庫Dialect
    • Eva_IOC >
      • IOC程式主體
      • AOP應用
    • Eva_Struct >
      • Struct範例
      • Bit Strcut範例
      • 指標範例
      • 於Eva_ORM中使用
    • Eva_Sort >
      • 依指定Properties排序
      • 部份排序
      • 優先排序部份
    • Eva_Cache >
      • hashing-based cache
      • 非hashing-based cache
  • API列表
  • 技術支援
    • 協助開發
  • 相關書籍

Bit Struct範例.

Eva_Struct提供BitStructMapper介面供使用者建立Bit Struct物件, 一個Bit Struct物件與一般物件沒什麼不同, 只是任何對該物件的修改都可能影響到該物件底層所使用到的一個byte陣列. 

Bit Struct物件可以被嵌入在Struct物件的欄位中, 當然Bit Struct物件也可以用於Eva_ORM中, 所有Bit Struct物件的Property, 在資料庫中也將以位元陣列的方式儲存. 
package com.logntw.eva.struct;

import junit.framework.Assert;

import org.junit.Test;

import com.logntw.eva.anno.bitstruct.Bits;
import com.logntw.eva.anno.struct.Bytes;
import com.logntw.eva.anno.struct.FieldSeq;
import com.logntw.eva.bitstruct.BitStructMapper;
import com.logntw.eva.bitstruct.defo.DefoBitStructMappers;
import com.logntw.eva.buf.MAByteBuffer;
import com.logntw.eva.buf.monitor.MonitoredBuf;
import com.logntw.eva.struct.defo.DefoStructMappers;

public class StructMKGeneratorTest7
{
  //2 + 3 + 1 = 6 bits = 1 byte
  @com.logntw.eva.anno.bitstruct.BitStruct
  public static interface Z
  {
    @FieldSeq(1)
    @Bits(2)
    char getA();
    void setA(char a);  
        
    @FieldSeq(3)
    @Bits(3)
    byte getB();
    void setB(byte b);
          
    @FieldSeq(5)    
    @Bits(1)
    void setC(byte c);
    byte getC();
  }
  
  // 6 + 6 + 2 = 14 bits = 2 bytes
  @com.logntw.eva.anno.bitstruct.BitStruct
  public static interface Y
  {
    @FieldSeq(2)
    @Bits(6)
    short getA();
    void setA(short a);
    
    @FieldSeq(4)
    @Bits(6)
    byte getB();
    void setB(byte b);
          
    @FieldSeq(6)
    @Bits(2)
    void setC(long c);
    long getC();
  }
  
  // 2 + 3 + 2 + 4 + 1 + 5 + 8 + 1 = 26
  @com.logntw.eva.anno.struct.Struct
  public static interface X
  {
    @FieldSeq(1)
    short getA();
    void setA(short a);
    
    @FieldSeq(2)
    @Bytes(3)
    String getB();
    void setB(String b);
    
    @FieldSeq(3)
    Y getC();
    void setC(Y c);
    
    @FieldSeq(4)
    int getD();
    void setD(int d);
    
    @FieldSeq(5)
    byte getE();
    void setE(byte e);
    
    @FieldSeq(6)
    @Bytes(5)
    String getF();
    void setF(String f);    
        
    @FieldSeq(7)    
    void setG(long g);
    long getG();
    
    @FieldSeq(8)
    Z getH();
    void setH(Z h);
  }
  
  
  @Test
  public void testStructMKGenerator() throws Exception
  {
    Assert.assertEquals(StructUtil.getTotalBytes(X.class), 26);
    Assert.assertEquals(StructUtil.getTotalBytes(Y.class), 2);
    Assert.assertEquals(StructUtil.getTotalBytes(Z.class), 1);
    
    StructMapper<X> g = DefoStructMappers.getMapper(X.class);
    MonitoredBuf buf = MAByteBuffer.Allocate(66);
    X a = g.ptr(buf);
    
    
    Assert.assertEquals(a.getA(), 0);
    Assert.assertEquals(a.getB(), "\0\0\0");
    Assert.assertEquals(a.getC().getA(), 0);
    Assert.assertEquals(a.getC().getB(), 0);
    Assert.assertEquals(a.getC().getC(), 0);
    Assert.assertEquals(a.getD(), 0);
    Assert.assertEquals(a.getE(), 0);
    Assert.assertEquals(a.getF(), "\0\0\0\0\0");
    Assert.assertEquals(a.getG(), 0);      
    Assert.assertEquals(a.getH().getA(), 0);
    Assert.assertEquals(a.getH().getB(), 0);
    Assert.assertEquals(a.getH().getC(), 0);
    
    
    
    a.setA((short)0x1111);
    a.setB("WXY");
    a.getC().setA((short)0x7070);
    a.getC().setB((byte)0x8383);
    a.getC().setC((byte)0x1515);  
    a.setD((int)0x22222222);
    a.setE((byte)0x33);
    a.setF("ABC");
    a.setG((long)0x1414141414141414l);  
    a.getH().setA((char)0x7676);
    a.getH().setB((byte)0x76);  
    a.getH().setC((byte)0x76);  
    a.setC(a.getC());
    a.setH(a.getH());
    

    Assert.assertEquals(a.getA(), 0x1111);
    Assert.assertEquals(a.getB(), "WXY");    
    Assert.assertEquals(a.getC().getA(), 0x30);
    Assert.assertEquals(a.getC().getB(), 0x03);
    Assert.assertEquals(a.getC().getC(), 0x01);
    Assert.assertEquals(a.getD(), 0x22222222);
    Assert.assertEquals(a.getE(), 0x33);
    Assert.assertEquals(a.getF(), "ABC\0\0");
    Assert.assertEquals(a.getG(), (long)0x1414141414141414l);
          
    Assert.assertEquals(a.getH().getA(), 2);
    Assert.assertEquals(a.getH().getB(), 6);
    Assert.assertEquals(a.getH().getC(), 0);
            

    BitStructMapper<Y> g2 = DefoBitStructMappers.getMapper(Y.class);    
    Y b = g2.createBitStruct();
        
    Assert.assertEquals(b.getA(), (short)0);
    Assert.assertEquals(b.getB(), (byte)0);
    Assert.assertEquals(b.getC(), 0l);
        
    b.setA((short)0x98);
    b.setB((byte)0x19);
    b.setC(0x53);
    
    Assert.assertEquals(b.getA(), 0x18);
    Assert.assertEquals(b.getB(), 0x19);   
    Assert.assertEquals(b.getC(), 0x3);
    
    a.setC(b);
    
    b.setA((short)0x22);

    Assert.assertEquals(b.getA(), (short)0x22);      
    Assert.assertEquals(a.getC().getA(), 0x18);
  }
  
  
}
Powered by Create your own unique website with customizable templates.