mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 01:20:25 +08:00
finished component: Identifier
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package component;
|
||||
|
||||
import encoding.Block;
|
||||
import encoding.Encoder;
|
||||
import encoding.IEncodingAble;
|
||||
import encoding.*;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
@@ -21,6 +20,80 @@ import java.util.List;
|
||||
class IdentifierComponentContainer{
|
||||
private List<IdentifierComponent> identifierComponents;
|
||||
|
||||
public IdentifierComponentContainer(){
|
||||
identifierComponents=new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有名称组件
|
||||
* @return
|
||||
*/
|
||||
public List<IdentifierComponent> getElement(){
|
||||
return identifierComponents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件个数
|
||||
* @return
|
||||
*/
|
||||
public int length(){
|
||||
return identifierComponents.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空组件
|
||||
*/
|
||||
public void clear(){
|
||||
identifierComponents.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个组件
|
||||
* @param identifierComponent
|
||||
*/
|
||||
public void addElement(IdentifierComponent identifierComponent){
|
||||
identifierComponents.add(identifierComponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定索引位置的组件
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent getElement(int index){
|
||||
if(index>=this.length()){
|
||||
return null;
|
||||
}
|
||||
return this.identifierComponents.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getPrefix(int num){
|
||||
if(num>this.length()){
|
||||
return null;
|
||||
}
|
||||
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
|
||||
for (int i = 0; i < num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
|
||||
public IdentifierComponentContainer getSubIdentifierContainer(int startIndex,int num){
|
||||
if((startIndex+num)>this.length()){
|
||||
return null;
|
||||
}
|
||||
|
||||
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
|
||||
for (int i = startIndex; i < startIndex+num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
@@ -75,20 +148,164 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
if((identifierString.length()<=0)||(!identifierString.startsWith("/"))){
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] componentStrings=identifierString.split("/");
|
||||
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
|
||||
//...
|
||||
this.components.clear();
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
this.components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 components 初始化当前 Identifier
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public boolean buildIdentifierByComponents(IdentifierComponentContainer container){
|
||||
if(container.length()<=0){
|
||||
return false;
|
||||
}
|
||||
|
||||
this.components.clear();
|
||||
for (int i = 0; i < container.length(); i++) {
|
||||
this.components.addElement(container.getElement(i));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对 Identifier 进行线速编码
|
||||
* // @Description:
|
||||
* // 1. 需要注意,本函数只对 Identifier 进行编码,不包含外层区分不同标识的Type,编码的格式如下:
|
||||
* // Identifier = 101 TLV-LENGTH
|
||||
* // *IdentifierComponent
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
for (int i = 0; i < components.length(); i++) {
|
||||
int tmpLen=components.getElement(i).wireEncode(encoder);
|
||||
if(tmpLen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmpLen;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// 编码 TLV-LENGTH
|
||||
int tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmpLen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmpLen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
|
||||
if(tmpLen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmpLen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 TLV Block 进行线速解码为一个 Identifier
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
return false;
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifier))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
this.components.clear();
|
||||
ElementContainer elementContainer=block.getSubElements();
|
||||
for (int i = 0; i < elementContainer.length(); i++) {
|
||||
this.components.addElement(new IdentifierComponent(elementContainer.getBlock(i)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Identifier表示成Uri字符串
|
||||
* // @Description:
|
||||
* // 1. https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
String res="";
|
||||
for (int i = 0; i < components.length(); i++) {
|
||||
res+=("/"+components.getElement(i).toUri());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否名称组件数量为空
|
||||
* @return
|
||||
*/
|
||||
public boolean empty(){
|
||||
return components.length()==0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本标识包含的名称组件数量
|
||||
* @return
|
||||
*/
|
||||
public int size(){
|
||||
return components.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本标识包含的第 index 个名称组件
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent get(int index){
|
||||
return this.components.getElement(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标识中所有的名称组件
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getComponents(){
|
||||
return this.components;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getPrefix(int num){
|
||||
return this.components.getPrefix(num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回从第 startIndex 个组件开始往后的 num 个组件组成的新标识
|
||||
* @return
|
||||
*/
|
||||
public Identifier getSubIdentifier(int startIndex,int num){
|
||||
return new Identifier(this.components.getSubIdentifierContainer(startIndex,num));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前标识是否有效
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid(){
|
||||
return this.components.length()>0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user