`
Copperfield
  • 浏览: 255256 次
  • 性别: Icon_minigender_1
  • 来自: 上海
博客专栏
C407adc3-512e-3a03-a056-ce4607c3a3c0
java并发编程陷阱
浏览量:24687
社区版块
存档分类

tapestry

 
阅读更多

tapestry

http://tapestry.apache.org/tapestry3/doc/api/index.html

http://archive.apache.org/dist/jakarta/tapestry/Tapestry-3.0.2-bin.zip

 

Mock Table utility for CRUD operations

While learning a new technology we may need a database table to store the data.
But creating a database, setting up jdbc connection and writing crud operation may be cumbersome.

So I thought it would be good to have some mock utility to represent a table which can be used just like a database table.

Here is what I came up with.

1 package com.sivalabs.sample.util;
2 import java.io.Serializable;
3
4 public interface Identifiable<K> extends Serializable
5 {
6   public void setId(K id);
7   public K getId();
8 }

 

01 package com.sivalabs.sample.util;
02
03 import java.util.Collection;
04 import java.util.HashMap;
05 import java.util.Map;
06
07 public abstract class Table<PK extends Object, T extends Identifiable<PK>>
08 {
09   protected final Map<PK, T> table = new HashMap<PK, T>();
10   public abstract PK getNextId();
11    
12   protected Table()
13   {
14   }
15    
16   public void create(T obj)
17   {
18    if (table.containsKey(obj.getId()))
19    {
20     throw new RuntimeException( "PrimaryKey [" +obj.getId()+ "] already exists" );
21    }
22    obj.setId(getNextId());
23    table.put(obj.getId(), obj);
24   }
25    
26   public Collection<T> getAll()
27   {
28    return table.values();
29   }
30    
31   public T getById(PK id)
32   {
33    return table.get(id);
34   }
35    
36   public void update(T obj)
37   {
38    if (!table.containsKey(obj.getId()))
39    {
40     throw new RuntimeException( "PrimaryKey [" +obj.getId()+ "] doesn't exists" );
41    }
42    table.put(obj.getId(), obj);
43   }
44    
45   public void delete(T obj)
46   {
47    delete(obj.getId());
48   }
49    
50   public void delete(PK id)
51   {
52    if (!table.containsKey(id))
53    {
54     throw new RuntimeException( "PrimaryKey [" +id+ "] doesn't exists" );
55    }
56    table.remove(id);
57   }
58 }


Let us create a pojo Message.java.

01 package com.sivalabs.sample;
02
03 import java.util.Date;
04 import com.sivalabs.sample.util.Identifiable;
05
06 public class Message implements Identifiable<Integer>
07 {
08   private static final long serialVersionUID = 1L;
09    
10   private Integer id;
11   private String text;
12   private String postedBy;
13   private Date postedDate = new Date();
14   public Message()
15   {
16   }
17    
18   public Message(Integer id, String text, String postedBy, Date postedDate)
19   {
20    this .id = id;
21    this .text = text;
22    this .postedBy = postedBy;
23    this .postedDate = postedDate;
24   }
25
26   public Integer getId()
27   {
28    return id;
29   }
30   public void setId(Integer id)
31   {
32    this .id = id;
33   }
34   //setters, getters for text, postedBy, postedDate
35 }


Now let us create a mock table for storing Messages.
The Message table needs to extend Table and provide what is the type of primary key and what type of objects MessageTable is going to contain using generics <Integer, Message>.

01 package com.sivalabs.sample.util;
02 import java.util.concurrent.atomic.AtomicInteger;
03 import com.sivalabs.sample.Message;
04
05 public class MessageTable extends Table<Integer, Message>
06 {
07   private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger( 0 );
08   @Override
09   public Integer getNextId()
10   {
11    return ATOMIC_INTEGER.incrementAndGet();
12   }
13 }


Now let us create a MessageService which holds an instance of MessageTable and expose the CRUD operations to clients.

01 package com.sivalabs.sample;
02
03 import java.util.Collection;
04 import java.util.Date;
05 import com.sivalabs.sample.util.MessageTable;
06
07
08 public class MessageService
09 {
10   private static final MessageTable MESSAGE_TABLE = new MessageTable();
11   static
12   {
13    MESSAGE_TABLE.create( new Message( 1 , "Message1 Text" , "Siva" , new Date()));
14    MESSAGE_TABLE.create( new Message( 2 , "Message2 Text" , "Prasad" , new Date()));
15    MESSAGE_TABLE.create( new Message( 3 , "Message3 Text" , "Prasad" , new Date()));
16    MESSAGE_TABLE.create( new Message( 4 , "Message4 Text" , "Siva" , new Date())); 
17   }
18    
19   public Collection<Message> getMessages()
20   {
21    return MESSAGE_TABLE.getAll();
22   }
23
24   public Message getMessage(Integer id)
25   {
26    return MESSAGE_TABLE.getById(id);
27   }
28
29   public void saveMessage(Message message)
30   {
31    MESSAGE_TABLE.create(message);
32   }
33
34   public void updateMessage(Message message)
35   {
36    MESSAGE_TABLE.update(message);
37   }
38
39   public void deleteMessage(Integer id)
40   {
41    MESSAGE_TABLE.delete(id);
42   }
43 }



Now if you want to create a mock table for another pojo User.java it is simple.

01 package com.sivalabs.sample.util;
02 import java.util.concurrent.atomic.AtomicInteger;
03 import com.sivalabs.sample.User;
04
05 public class UserTable extends Table<Integer, User>
06 {
07   private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger( 0 );
08   @Override
09   public Integer getNextId()
10   {
11    return ATOMIC_INTEGER.incrementAndGet();
12   }
13 }


If the primary key is always an auto incremented integer value we can move getNextId() method to Table.java. Then creating mock table becomes even more simpler.

1 package com.sivalabs.sample.util;
2 import com.sivalabs.sample.User;
3
4 public class UserTable extends Table<Integer, User>
5 {
6     
7 }
分享到:
评论

相关推荐

    tapestry教程资料文档合集

    Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...

    tapestry5以上的帮助事例,帮助文档与spring衔接文档

    Tapestry是一个基于控件的框架以致于用它开发Web应用类似开发传统的GUI应用。你用Tapestry开发Web应用时你无需关注以操作为中心的(Operation-centric) Servlet API.引用Tapestry网站上的一句话:"Tapestry用对象...

    深入浅出Tapestry

    资源名称:深入浅出Tapestry内容简介:本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在...

    深入浅出tapestry

    本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在J2EE Web应用程序中应用Tapestry框架的...

    Tapestry5最新中文入门实例教程

    本文介绍Tapestry框架版本5。本文利用Tapestry 5开发一个简单的具有创建/读/更新/删除功能的应用,在创建这个应用的过程中,本文体会到Tapestry带来的开发效率的提升。从多方面来讲解 Tapestry应用,比如应用的...

    tapestry官方中文文档

    Tapestry 4 官方文档中文版本,现在中文资料比较少,和大家共享一下

    Tapestry5.0.16_API文档

    Tapestry5.0.16文档和大家一起学习

    Maven + Tapestry5.3.8 + Spring4.0.5 + Oracle10g

    这是Tapestry5.3.8 版本的一个大Demo,集合Spring4.0, 采用Maven 项目管理工具,没有集合Hibernate。 之所以说是个大Demo,是因为这项目中包含的内容并不少,包含: 1)解决了Tapestry5.3.8中文Bug问题 2)Tapestry...

    tapestry3开发指南,带tapestry3所有jar包

    tapestry3开发指南,带tapestry3所有jar包

    tapestry-bin-5.1.0.5

    tapestry-bin-5.1.0.5

    Tapestry4开发指南

    Tapestry4的雏形是Tapestry3.1,Tapestry的作者Howard,不光开发了Tapestry,还同时开发了一个轻量级框架Hivemind。所以Tapestry3.1的开发一开始就处于Hivemind框架之下。后来由于种种原因,Howard没有将Tapestry3.1...

    tapestry5.2.6 jar包

    tapestry5.2.6 最新jar包,Tapestry框架是一个位于java servlet容器和Tapestry应用程序之间的层

    Tapestry开发指南

    Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得java代码与html...

    tapestry技术

    tapestry技术 Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得...

    tapestry学习入门资料

    强大的tapestry框架的学习入门资料,详细介绍了tapestry的常用开发文档。

    tapestry-src-5.1.0.5.zip

    包含: tapestry 的源代码, tapestry集成spring2.0 tapestry快速启动 tapestry upload tapestry hibernate tapestry annotations

    tapestry5中文文档

    tapestry5组件说明使用及登陆修改等简单实例

    Tapestry 5开发指南(英文)

    Tapestry 5开发指南(英文) Tapestry 5开发指南(英文) Tapestry 5开发指南(英文) Tapestry 5开发指南(英文)

    Tapestry通用WEB框架

    Tapestry通用WEB框架支持,切换皮肤,自主分页,

Global site tag (gtag.js) - Google Analytics