KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > ConfigurationCache


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ConfigurationCache.java,v 1.2 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19
20 import org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.Escape;
21
22
23 /**
24  * This class represents a configuration that can be used to enable caching and therefore improves performance
25  * of EMF serialization.
26  * This class is considered an INTERNAL API and might change in the future.
27  * @since 2.1.0
28  */

29 public class ConfigurationCache
30 {
31   public static final ConfigurationCache INSTANCE = new ConfigurationCache();
32
33   protected static final int SIZE = 100;
34
35   protected XMLString printers[] = new XMLString [SIZE];
36
37   protected Escape escapes[] = new Escape [SIZE];
38
39   protected int freePrinterIndex = -1;
40
41   protected int freeEscapeIndex = -1;
42
43   protected int currentSize = SIZE;
44
45   protected ConfigurationCache()
46   {
47   }
48
49   protected synchronized XMLString getPrinter()
50   {
51     if (freePrinterIndex < 0)
52     {
53       return new XMLString();
54     }
55     XMLString printer = printers[freePrinterIndex];
56     printers[freePrinterIndex--] = null;
57     return printer;
58   }
59
60   protected synchronized void releasePrinter(XMLString printer)
61   {
62     ++freePrinterIndex;
63     if (printers.length == freePrinterIndex)
64     {
65       currentSize += SIZE;
66       XMLString newarray[] = new XMLString [currentSize];
67       System.arraycopy(printers, 0, newarray, 0, printers.length);
68       printers = newarray;
69     }
70     printers[freePrinterIndex] = printer;
71   }
72
73   protected synchronized Escape getEscape()
74   {
75     if (freeEscapeIndex < 0)
76     {
77       return new Escape();
78     }
79     Escape escape = escapes[freeEscapeIndex];
80     escapes[freeEscapeIndex--] = null;
81     return escape;
82   }
83
84   protected synchronized void releaseEscape(Escape escape)
85   {
86     ++freeEscapeIndex;
87     if (escapes.length == freeEscapeIndex)
88     {
89       currentSize += SIZE;
90       Escape newarray[] = new Escape [currentSize];
91       System.arraycopy(escapes, 0, newarray, 0, escapes.length);
92       escapes = newarray;
93     }
94     escapes[freeEscapeIndex] = escape;
95   }
96
97   public synchronized void release()
98   {
99     freeEscapeIndex = -1;
100     freePrinterIndex = -1;
101     for (int i = 0; i < printers.length; i++)
102     {
103       printers[i] = null;
104     }
105     for (int i = 0; i < escapes.length; i++)
106     {
107       escapes[i] = null;
108     }
109   }
110
111 }
112
Popular Tags