Source Code Cross Referenced for Time.java in  » Collaboration » JacORB » org » jacorb » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » JacORB » org.jacorb.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jacorb.util;
002:
003:        /*
004:         *        JacORB - a free Java ORB
005:         *
006:         *   Copyright (C) 2002-2004 Gerald Brose
007:         *
008:         *   This library is free software; you can redistribute it and/or
009:         *   modify it under the terms of the GNU Library General Public
010:         *   License as published by the Free Software Foundation; either
011:         *   version 2 of the License, or (at your option) any later version.
012:         *
013:         *   This library is distributed in the hope that it will be useful,
014:         *   but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         *   Library General Public License for more details.
017:         *
018:         *   You should have received a copy of the GNU Library General Public
019:         *   License along with this library; if not, write to the Free
020:         *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021:         *
022:         */
023:
024:        import org.omg.TimeBase.*;
025:        import org.jacorb.orb.*;
026:
027:        /**
028:         * Contains static methods to handle CORBA time values.
029:         *
030:         * @author Andre Spiegel <spiegel@gnu.org>
031:         * @version $Id: Time.java,v 1.10 2006/07/07 10:57:59 alphonse.bendt Exp $
032:         */
033:        public class Time {
034:            private Time() {
035:                // utility class
036:                super ();
037:            }
038:
039:            /**
040:             * Difference between the CORBA Epoch and the Unix Epoch: the time
041:             * from 1582/10/15 00:00 until 1970/01/01 00:00 in 100 ns units.
042:             */
043:            public static final long UNIX_OFFSET = 122192928000000000L;
044:
045:            /**
046:             * Returns the current time as a CORBA UtcT.
047:             */
048:            public static UtcT corbaTime() {
049:                return corbaTime(System.currentTimeMillis());
050:            }
051:
052:            /**
053:             * Converts the given unixTime into a CORBA UtcT.
054:             * @param unixTime the number of milliseconds since 1970/01/01 00:00 UTC.
055:             */
056:            public static UtcT corbaTime(long unixTime) {
057:                UtcT result = new UtcT();
058:
059:                result.time = (unixTime * 10000) + UNIX_OFFSET;
060:
061:                // unixTime is always UTC.
062:                // Therefore, no time zone offset.
063:                result.tdf = 0;
064:
065:                // nothing reasonable to put here
066:                result.inacchi = 0;
067:                result.inacclo = 0;
068:
069:                return result;
070:            }
071:
072:            /**
073:             * Converts the given Java date into a CORBA UtcT.
074:             */
075:            public static UtcT corbaTime(java.util.Date date) {
076:                return corbaTime(date.getTime());
077:            }
078:
079:            /**
080:             * Returns a CORBA UtcT that represents an instant that lies
081:             * a given number of CORBA time units (100 ns) in the future.
082:             * If the argument is negative, returns null.
083:             */
084:            public static UtcT corbaFuture(long corbaUnits) {
085:                if (corbaUnits < 0) {
086:                    return null;
087:                }
088:
089:                UtcT result = corbaTime();
090:                result.time = result.time + corbaUnits;
091:                return result;
092:            }
093:
094:            /**
095:             * Returns the number of milliseconds between now and the given CORBA
096:             * time.  The value is positive if that time is in the future, and
097:             * negative otherwise.
098:             */
099:            public static long millisTo(UtcT time) {
100:                long unixTime = (time.time - UNIX_OFFSET) / 10000;
101:
102:                // if the time is not UTC, correct time zone
103:                if (time.tdf != 0) {
104:                    unixTime = unixTime - (time.tdf * 60000);
105:                }
106:
107:                return unixTime - System.currentTimeMillis();
108:            }
109:
110:            /**
111:             * Returns true if the instant represented by the given UtcT is
112:             * already in the past, false otherwise.  As a special convenience,
113:             * this method also returns false if the argument is null.
114:             */
115:            public static boolean hasPassed(UtcT time) {
116:                if (time != null) {
117:                    return millisTo(time) < 0;
118:                }
119:
120:                return false;
121:            }
122:
123:            /**
124:             * Compares two UtcT time values and returns that which is earlier.
125:             * Either argument can be null; this is considered as a time that
126:             * lies indefinitely in the future.  If both arguments are null,
127:             * this method returns null itself.
128:             */
129:            public static UtcT earliest(UtcT timeA, UtcT timeB) {
130:                if (timeA == null) {
131:                    if (timeB == null) {
132:                        return null;
133:                    }
134:                    return timeB;
135:                }
136:
137:                if (timeB == null || timeA.time <= timeB.time) {
138:                    return timeA;
139:                }
140:                return timeB;
141:            }
142:
143:            /**
144:             * Returns a CDR encapsulation of the given UtcT.
145:             */
146:            public static byte[] toCDR(UtcT time) {
147:                // TODO: make this more efficient with mere bit shifting
148:                byte[] buffer = new byte[25];
149:                CDROutputStream out = new CDROutputStream(buffer);
150:                out.beginEncapsulatedArray();
151:                UtcTHelper.write(out, time);
152:                return out.getBufferCopy();
153:            }
154:
155:            /**
156:             * Decodes a CDR encapsulation of a UtcT.
157:             */
158:            public static UtcT fromCDR(byte[] buffer) {
159:                CDRInputStream in = new CDRInputStream(null, buffer);
160:                in.openEncapsulatedArray();
161:                return UtcTHelper.read(in);
162:            }
163:
164:            /**
165:             * This method blocks until the given time has been reached.
166:             * If the time is null, or it has already passed,
167:             * then this method returns immediately.
168:             */
169:            public static void waitFor(UtcT time) {
170:                if (time != null) {
171:                    long now = System.currentTimeMillis();
172:                    long delta = Time.millisTo(time);
173:                    long then = now + delta;
174:
175:                    while (delta > 0) {
176:                        try {
177:                            Thread.sleep(delta);
178:                        } catch (InterruptedException e) {
179:                            // ignored
180:                        }
181:
182:                        delta = then - System.currentTimeMillis();
183:                    }
184:                }
185:            }
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.