📄 tinyspace.java
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2008 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */package org.jpos.space;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.io.Serializable;/** * Tiny Space implementation * * @author Alejandro Revilla * @version $Revision: 2594 $ $Date: 2008-01-22 14:41:31 -0200 (Tue, 22 Jan 2008) $ * @since 1.4.7 */public class TinySpace implements Space, Serializable { protected Map map = new HashMap (); public TinySpace () { super(); } public void out (Object key, Object value) { synchronized (this) { Object v = map.get (key); if (v == null) map.put (key, value); else if (v instanceof Data) ((Data)v).add (value); else { Data data = new Data(); data.add (v); data.add (value); map.put (key, data); } this.notifyAll (); } } public void out (Object id, Object value, long timeout) { out (id, new LeasedReference (value, timeout)); } public synchronized Object rdp (Object key) { Object obj = map.get (key); if (obj instanceof Data) obj = ((Data) obj).get (key); else if (obj instanceof LeasedReference) { obj = ((LeasedReference)obj).get (); } return obj; } public synchronized Object inp (Object key) { Object obj = map.get (key); if (obj instanceof Data) { Data data = (Data) obj; obj = data.remove (); if (data.isEmpty ()) map.remove (key); } else if (obj != null) { if (obj instanceof LeasedReference) { obj = ((LeasedReference)obj).get (); } map.remove (key); } return obj; } public synchronized Object in (Object key) { Object obj; while ((obj = inp (key)) == null) { try { this.wait (); } catch (InterruptedException e) { } } return obj; } public synchronized Object in (Object key, long timeout) { Object obj; long now = System.currentTimeMillis(); long end = now + timeout; while ((obj = inp (key)) == null && ((now = System.currentTimeMillis()) < end)) { try { this.wait (end - now); } catch (InterruptedException e) { } } return obj; } public synchronized Object rd (Object key) { Object obj; while ((obj = rdp (key)) == null) { try { this.wait (); } catch (InterruptedException e) { } } return obj; } public synchronized Object rd (Object key, long timeout) { Object obj; long now = System.currentTimeMillis(); long end = now + timeout; while ((obj = rdp (key)) == null && ((now = System.currentTimeMillis()) < end)) { try { this.wait (end - now); } catch (InterruptedException e) { } } return obj; } public void push (Object id, Object value) { throw new SpaceError ("Unsupported operation"); } public void push (Object id, Object value, long timeout) { throw new SpaceError ("Unsupported operation"); } protected static final class Data { LinkedList data; protected Data () { super(); data = new LinkedList (); } protected Data (Object value) { this (); add (value); } protected void add (Object value) { data.add (value); } protected Object get (Object value) { Object obj = null; while (size() > 0) { obj = data.getFirst(); if (obj instanceof LeasedReference) { obj = ((LeasedReference)obj).get (); if (obj == null) { data.removeFirst (); continue; } } break; } return obj; } protected int size () { return data.size (); } protected Object remove () { Object obj = null; while (size() > 0) { obj = data.removeFirst(); if (obj instanceof LeasedReference) { LeasedReference ref = (LeasedReference) obj; obj = ref.get (); if (obj == null) { continue; } ref.discard (); } break; } return obj; } protected boolean isEmpty () { return data.isEmpty (); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -