首页 \ 问答 \ 向服务提供者公开Java服务提供者接口(Expose a Java Service Provider Interface to Service Providers)

向服务提供者公开Java服务提供者接口(Expose a Java Service Provider Interface to Service Providers)

我正在尝试为插件/应用程序创建一个框架,我想我已经决定使用Java中的服务提供程序接口(SPI)来实现这一点。 最终的愿景是人们可以下载插件(如应用程序商店),然后将jar放入文件夹,他们将使用现有程序。

所以我有一个PluginInterface类,它将成为我的SPI。 我希望所有插件都实现这个类(它有基本方法,如doesAccepthandle等)。 由于第三方将创建服务提供者, 我如何让他们在他们的代码中引用这个Java接口(以便他们可以实现它),而不实际给他们主应用程序的源代码?


I'm trying to create a framework for plugin/apps, and I think I've settled on using a Service Provider Interface (SPI) in Java to achieve that. The ultimate vision is that people can download plugins (like an app store) and then just drop the jars into a folder, and they'll work with the existing program.

So I have a class PluginInterface that will be my SPI. I want all plugins to implement this class (it has basic methods like doesAccept, handle, etc.). Since 3rd parties will be creating the Service Providers, how can I let them reference this Java interface in their code (so they can implement it), without actually giving them the source code of the main application?


原文:https://stackoverflow.com/questions/30135177
更新时间:2023-05-06 11:05

最满意答案

现有答案中有很好的信息,但让我尝试一个实用的总结:

System.DateTime.ToFileTime()System.DateTime.ToFileTimeUtc()之间的区别唯一重要的是当实例的.Kind属性等于Unspecified ,即,如果不清楚时间值是表示本地时间还是UTC时间。
这个信息实际上是Hans Passant的一个答案,他自己删除了。

换句话说:对于其.Kind属性等于LocalUtc (唯一其他可能的值)的System.DateTime实例, System.DateTime.ToFileTime()System.DateTime.ToFileTimeUtc()行为相同 - 这是OP经历的,因为他的输入值是.Kind LocalFile.GetCreationTime()返回的那种)。

请注意,设计的相关System.DateTimeOffset类型始终带有显式时区信息,因此从不存在歧义,这就是为什么该类型只有 .ToFileTime()方法。

正如其他人所说, 返回始终代表UTC时间

  • 根据定义,文件时间值总是表示UTC中的时间点。

  • 因此, .ToFileTime().ToFileTimeUtc()创建一个时间点,表示为自1601年1月1日午夜以来100ns间隔的计数。

为了补充OP自己的例子,其中.ToFileTime().ToFileTimeUtc()的结果不同:

// Create a DateTime instance with .Kind equal to Unspecified;
// use the earliest date that can be represented as a file time.
DateTime dtUnspecified = DateTime.Parse("1601-01-01");

Console.WriteLine(
    dtUnspecified.ToFileTime() + "\n" +
    dtUnspecified.ToFileTimeUtc()
);

美国东部时区的样本输出:

180000000000  // 1601-01-01T05:00:00Z - 5 AM UTC
0             // 1601-01-01T00:00:00Z - midnight UTC

There's good information in the existing answers, but let me attempt a pragmatic summary:

The only time the distinction between System.DateTime.ToFileTime() and System.DateTime.ToFileTimeUtc() matters is when the instance's .Kind property equals Unspecified, i.e., if it's unclear whether the time value represents a local or UTC time.
This information was actually in an answer by Hans Passant that he himself deleted.

In other word: for System.DateTime instances whose .Kind property equals either Local or Utc (the only other possible values), System.DateTime.ToFileTime() and System.DateTime.ToFileTimeUtc() behave identically - this is what the OP experienced, because his input value was of .Kind Local (the kind returned by File.GetCreationTime()).

Note that the related System.DateTimeOffset type by design always carries explicit time zone information, so there's never ambiguity, which is why that type only has a .ToFileTime() method.

As others have noted, the return value always represents a UTC time:

  • File time values by definition, invariably represent a point in time in UTC.

  • Thus, both .ToFileTime() and .ToFileTimeUtc() create a point in time expressed as the count of 100ns intervals since midnight January 1, 1601 UTC.

To complement the OP's own example with one where .ToFileTime() and .ToFileTimeUtc() do differ in outcome:

// Create a DateTime instance with .Kind equal to Unspecified;
// use the earliest date that can be represented as a file time.
DateTime dtUnspecified = DateTime.Parse("1601-01-01");

Console.WriteLine(
    dtUnspecified.ToFileTime() + "\n" +
    dtUnspecified.ToFileTimeUtc()
);

Sample output in the US Eastern time zone:

180000000000  // 1601-01-01T05:00:00Z - 5 AM UTC
0             // 1601-01-01T00:00:00Z - midnight UTC

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。