状态设计模式之实践。

需求如下:

These are the filters, in priority order:

1- GetCardInput.AcqIndexList:

    If application send this list, then the library must search just for these index in the table.

    For example:

        GetCardInput.AcqIndexList = ('1701', '1702', '1703').

    In this case, if the card inserted/swiped does not match with these three rows, library must return GetCardOutput.ReturnCode = CARDAPPNAV.

IF GetCardInput.AcqIndexList == null, then try to filter by CardAppType:

2- GetCardInput.CardAppType:

    If application sends GetCardInput.CardAppType == 1, then the library must filter all the table by app type and return just rows with app type == 1.

    If application sends GetCardInput.CardAppType == 2, then the library must filter all the table by app type and return just rows with app type == 2.

    If the card inserted/swiped does not match, library must return GetCardOutput.ReturnCode = CARDAPPNAV.

    If application sends GetCardInput.CardAppType == 99, then the library use the hole table rows.

IF GetCardInput.CardAppType == 0, then try to filter by AcquirerId:

3- GetCardInput.AcquirerId:

    In this case, the library must filter all the table by acquirer id;

4- If application sends GetCardInput.CardAppType != 0 and Acquirer Id != 0, then library must use the both values to filter the table.

1.在把AID添加到内核的时候执行添加动作。进入过滤状态设计模式。退出的时候就已经把值赋值好了。

首先,需要设置context类,这个类包括数据库句柄。

GetCardInput inputData;

List<AidInfo> filteredTable;用于保存从数据库里查询出来的AID及优先级。

public boolean addAidList() {cardFilterContext.setState(new InitialState(cardFilterContext));try {while (true) {cardFilterContext.applyFilter();if (cardFilterContext.getState() instanceof QuitFiltertable) {// Logic for quitting CVMLogs.d(TAG, "QuitFiltertable");addAidToEmvList();return true;}}} catch (Exception e) {e.printStackTrace();}return false;

其中,抽象状态类。要设置成抽象,因为还有公共的实现函数。并且把context类放在里面,

便于具体状态类继承并使用。

public abstract class State {protected CardFilterContext cardFilterContext;public State(CardFilterContext cardFilterContext ) {this.cardFilterContext = cardFilterContext;}static final String TAG = InitializeAppState.class.getSimpleName();public abstract void applyFilter();public  List<AidInfo> processQueryResult(List<Map<String, Object>> queryResult) {List<AidInfo> aidInfoList = new ArrayList<>();for (Map<String, Object> result : queryResult) {// 根据实际情况处理每一行结果,并转换为 AidInfo 对象String aidValue = (String) result.get(EmvDbHelper.aid);int priority = (int) result.get(EmvDbHelper.aidPriority);
//            byte priority = (byte) result.get(EmvDbHelper.aidPriority);Logs.d(TAG,"aidValue "+ aidValue);Logs.d(TAG,"aidPriority "+ priority);AidInfo aidInfo = new AidInfo(aidValue, (byte)priority);aidInfoList.add(aidInfo);}return aidInfoList;}
}

3.具体状态类。

public class InitialState extends State {private final String TAG = "InitialState";public InitialState(CardFilterContext context) {super(context);}@Overridepublic void applyFilter() {if (cardFilterContext.inputData.getAcqIndexList() != null && !cardFilterContext.inputData.getAcqIndexList().isEmpty()) {// Execute AcqIndexList filterList<String> acqIndexList = cardFilterContext.inputData.getAcqIndexList();List<Map<String, Object>> queryResult = queryDbWithAcqIndexList(acqIndexList,cardFilterContext.getDbManager());List<AidInfo> aidInfoList = processQueryResult(queryResult);cardFilterContext.setFilteredTable(aidInfoList);cardFilterContext.setState(new QuitFiltertable(cardFilterContext));} else {// Transition to CardAppType statecardFilterContext.setState(new CardAppTypeState(cardFilterContext));}}public List<Map<String, Object>> queryDbWithAcqIndexList(List<String> acqIndexList,DbManager dbManager) {String[] selectionColumns = {EmvDbHelper.acquirerId, EmvDbHelper.aidIndex};String[] columnsToSelect = {EmvDbHelper.aid, EmvDbHelper.aidPriority};List<Map<String, Object>> result = new ArrayList<>();// 初始化一个空数组String[] selectionValues = new String[2];// 遍历 acqIndexList,对每个字符串进行拆分并查询for (String acqIndex : acqIndexList) {// 进行拆分逻辑,这里示例取前两个字符if (acqIndex.length() >= 4) {// 取前两个字符作为第一个值String part1 = acqIndex.substring(0, 2);// 取后两个字符作为第二个值String part2 = acqIndex.substring(2, 4);// 将拆分后的值赋值给 selectionValues 数组selectionValues[0] = part1;selectionValues[1] = part2;// 进行查询并将结果添加到结果集List<Map<String, Object>> queryResult = dbManager.getColumnMapValuesWithCondition(selectionColumns, selectionValues, columnsToSelect);result.addAll(queryResult);} else {Logs.e(TAG,"acqIndex.length() "+ acqIndex.length());break;}}// 返回查询结果return result;}}

还有一个需求就是返回ReturnCode = CARDAPPNAV.

如果拿到选择应用的结果就返回并设置对应的ReturnCode

 public boolean Appselect() {ContextAppSelete contextAppSelete = new ContextAppSelete();contextAppSelete.setState(new SelectAppState(contextAppSelete));try {while (true) {contextAppSelete.executeSelectFinalApp();if (contextAppSelete.getState() instanceof QuitFinalAppState) {contextAppSelete.executeSelectFinalApp();if (contextAppSelete.getErrorCode()!=0) {getCardOutput.setReturnCode(contextAppSelete.getReturnCodes());return false;} else {emv_candlist = contextAppSelete.getEmvCandlist();Logs.d(TAG, "emv_candlist.aid " + tools.hexString(emv_candlist.aid, 0, emv_candlist.aidLen));return true;}}}} catch (Exception e) {e.printStackTrace();}return false;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/447851.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

骨传导耳机哪个好?骨传导耳机最新品牌推荐

随着技术的迅猛发展&#xff0c;我们对于音质和舒适度的追求也变得越发强烈。作为耳机市场的一个新兴分支&#xff0c;骨传导耳机因其独特的传声方式——通过头骨传导声音而非经由耳道&#xff0c;这种方式不仅保留了音质的纯净&#xff0c;还避免了长时间戴耳机对听觉通道造成…

oracle 19c上安装样例数据库

样例schema的分类 HR: Human Resources OE: Order Entry PM: Product Media IX: Information Exchange SH: Sales History BI: Business Intelligence 安装样例数据库 1&#xff1a;HR的安装&#xff0c;通过dbca时候 2&#xff1a;HR的安装&#xff0c;安装完数据库后&#…

巧用windows的move命令结合jenkins让npm构建速度上来

一.背景 jenkins构建vue2的项目&#xff0c;每次都很慢&#xff0c;即便非常顺利都要1分多钟。之前的文章想到的私服的方式&#xff0c;windows下利用verdaccio私服让npm构建速度上来-CSDN博客。其实慢的原因不仅仅是依赖下载的问题。 二.还有哪里慢 1.源代码同步慢 从构建…

Linux多线程服务端编程:使用muduo C++网络库 学习笔记 第十一章 反思C++面向对象与虚函数(下)

11.7.2 值语义与生命期 值语义的一个巨大好处是生命期管理很简单&#xff0c;就跟int一样——你不需要操心int的生命期。值语义的对象要么是stack object&#xff0c;要么直接作为其他object的成员&#xff0c;因此我们不用担心它的生命期&#xff08;一个函数使用自己stack上…

瑞云效果图渲染客户端更新内容

瑞云渲染「效果图客户端」正式更新啦&#xff0c;在云渲染是影视动画、效果图用户都在选择的服务&#xff0c;云渲染的文件可通过网页端、客户端、插件端提交。随着新的一年到来&#xff0c;效果图云渲染客户端进行了一个较大的UI更新&#xff0c;还对功能、版本转换功能、作业…

前端常见的栈溢出报错

什么是栈溢出&#xff1f; 在前端开发中&#xff0c;栈溢出是指JavaScript引擎执行代码时&#xff0c;调用栈&#xff08;call stack&#xff09;变得太大&#xff0c;超过了浏览器或JavaScript引擎所分配的栈空间&#xff0c;从而导致栈溢出错误。调用栈是一种数据结构&#x…

【复现】帮管客CRM 客户管理系统 sql注入漏洞_37

目录 一.概述 二 .漏洞影响 三.漏洞复现 1. 漏洞一&#xff1a; 四.修复建议&#xff1a; 五. 搜索语法&#xff1a; 六.免责声明 一.概述 帮管客CRM客户管理系统基于先进的CRM营销理念设计&#xff0c;集客户档案、销售记录、业务往来于一身&#xff0c;以凝聚客户关系…

Vue.js 中子组件向父组件传值的方法

Vue.js 是一款流行的 JavaScript 前端框架&#xff0c;它提供了一套完整的工具和 API&#xff0c;使得开发者可以更加高效地构建交互式的 Web 应用程序。其中&#xff0c;组件化是 Vue.js 的一个核心概念&#xff0c;通过组件化可以将一个复杂的应用程序拆分成多个独立的部分&a…

简述MinewSemi的GNSS模块引领体育与健康科技革新

体育与健康科技领域的创新一直在推动人们更健康、更活跃的生活方式。创新微公司的GNSS模块正成为这一变革的关键推动力。本文将深入研究MinewSemi的GNSS模块在体育和健康追踪领域的创新应用&#xff0c;探讨其如何帮助个体更全面地了解和改善自己的身体状态。 1. 个性化运动轨迹…

UnitySahder实现Phong/BlinnPhong模型

目录 Phong模型公式&#xff1a; BlinnPhong模型公式&#xff1a; 实现&#xff1a; Phong模型&#xff1a; BlinnPhong模型&#xff1a; Phong模型公式&#xff1a; 结果自发光&#xff0b;环境光漫反射高光反射 BlinnPhong模型公式&#xff1a; 对Phong模型的简单修改&…

# Redis 分布式锁如何自动续期

Redis 分布式锁如何自动续期 何为分布式 分布式&#xff0c;从狭义上理解&#xff0c;也与集群差不多&#xff0c;但是它的组织比较松散&#xff0c;不像集群&#xff0c;有一定组织性&#xff0c;一台服务器宕了&#xff0c;其他的服务器可以顶上来。分布式的每一个节点&…

拓扑排序算法

操作对象&#xff1a;AOV网的点和边 有向无环图&#xff1a;有向图且不会形成回路 AOV网&#xff1a;在一个表示工程的有向图中&#xff0c;用顶点表示活动&#xff0c;用弧表示活动之间的优先关系&#xff0c;这样的有向图为顶点表示活动的网&#xff0c;称为AOV网 拓扑排序…